【发布时间】:2020-11-06 03:23:56
【问题描述】:
我正在尝试在 React Js 中创建登录和注册页面,其想法是它们应该呈现在同一页面上,并且用户应该能够在它们之间切换(类似于 example)
我可以在组件之间创建切换功能(使用useState),但问题是我还需要根据用户的选择更改url(如果用户想登录,url应该是“/login”,如果签名-向上 =>“/注册”)。我使用 React Router 为 Login 和 Signup 创建了单独的组件,但它们在不同的页面上打开,我希望实现的是将它们都放在同一个 Login 页面上。
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>
);
}
我对 React Js 还很陌生,所以我不知道这是否可能。我将不胜感激任何建议和帮助,谢谢。
【问题讨论】:
-
我很困惑,你说你想在同一个页面上有 2 个页面...这意味着它们不是 2 个页面,它们是 2 个不同的视图单页。只拥有
<Route exact path="/login" component={Login} />和<Route exact path="/sign-up" component={SignUp} />并使用react-router-dom的历史功能在路线之间移动有什么问题?
标签: reactjs react-router toggle