I'm trying to create a Login and Sign Up page in React Js, the idea is that that they should be rendered on the same page, and user should be able to switch between them (something like this example)
I can create toggle function between components(using useState), but the problem is that I need the url to change according to the user's choice also( if user wants to login, url should be "/login", if sign-up => "/signup"). I created separate components for Login and Signup with React Router, but they're opening on separate pages, what I would love to achieve is to have both of them on the same Login page.
import React from "react";
import "./styles.css";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Login from "./Login";
import SignUp from "./SignUp";
export default function App() {
return (
<div className="App">
<Router>
<Switch>
<Route exact path="/">
<Login />
</Route>
<Route exact path="/sign-up" component={SignUp} />
</Switch>
</Router>
</div>
);
}
import React from "react";
import { Link } from "react-router-dom";
export default function Login() {
return (
<div style={{ display: "flex", flexDirection: "column" }}>
<div style={{ display: "flex", justifyContent: "space-evenly" }}>
<Link to="/">
<h1>Login</h1>
</Link>
<Link to="/sign-up">
<h1>Sign Up</h1>
</Link>
</div>
<form
style={{ display: "flex", flexDirection: "column", padding: "40px" }}
>
<input type="email" placeholder="email"></input>
<input type="password" placeholder="password"></input>
</form>
</div>
);
}
import React from "react";
export default function SignUp() {
return (
<div>
<form style={{display:"flex", flexDirection:"column", padding:"40px"}}>
<input type ="name" placeholder="Name"></input>
<input type ="email" placeholder="email"></input>
<input type ="password" placeholder="password"></input>
</form>
</div>
);
}
I'm fairly new to React Js, so I don't know if this is even possible. I would appreciate any suggestion and help, thank you.