【问题标题】:How to switch between 2 components on the same page?如何在同一页面上的 2 个组件之间切换?
【发布时间】:2020-11-06 03:23:56
【问题描述】:

我正在尝试在 React Js 中创建登录和注册页面,其想法是它们应该呈现在同一页面上,并且用户应该能够在它们之间切换(类似于 example

我可以在组件之间创建切换功能(使用useState),但问题是我还需要根据用户的选择更改url(如果用户想登录,url应该是“/login”,如果签名-向上 =>“/注册”)。我使用 React Router 为 Login 和 Signup 创建了单独的组件,但它们在不同的页面上打开,我希望实现的是将它们都放在同一个 Login 页面上。

my code in CodeSandbox

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 个不同的视图单页。只拥有&lt;Route exact path="/login" component={Login} /&gt;&lt;Route exact path="/sign-up" component={SignUp} /&gt; 并使用react-router-dom 的历史功能在路线之间移动有什么问题?

标签: reactjs react-router toggle


【解决方案1】:

分别创建一个Header组件并在两个地方(登录和注册)组件中导入。

例子

Header.js

export default function Header() {
  return (
    <div style={{ display: "flex", justifyContent: "space-evenly" }}>
    <Link to="/">
      <h1>Login</h1>
    </Link>

    <Link to="/sign-up">
      <h1>Sign Up</h1>
    </Link>
  </div>
  )
}

现在在登录和注册组件中导入它

Login.js

<div style={{ display: "flex", flexDirection: "column" }}>
     <Header />

      <form
        style={{ display: "flex", flexDirection: "column", padding: "40px" }}
      >
        <input type="email" placeholder="email"></input>
        <input type="password" placeholder="password"></input>
      </form>
    </div>

SignUp.js

<div>
     <Header />
      <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>

更新Live working demo

【讨论】:

    【解决方案2】:

    首先,不要在登录组件中包含“登录”和“注册”链接。将它们移动到您的 App.js。

    App.js

    <div className="App">
      <Router>
        <Switch>
          <Route path="/">
            <div style={{ display: "flex", justifyContent: "space-evenly" }}>
              <Link to="/login">
                <h1>Login</h1>
              </Link>
    
              <Link to="/sign-up">
                <h1>Sign Up</h1>
              </Link>
            </div>
    
            <Route exact path="/sign-up" component={SignUp} />
            <Route exact path="/Login" component={Login} />
          </Route>
        </Switch>
      </Router>
    </div>
    

    请注意,&lt;Route exact path='/'&gt; 中的exact 必须被删除,以便您可以在路由更改时切换并呈现登录或注册页面。您可以随时参考react-router tutorial

    对于登录组件,只需包含表单(就像您为注册组件所做的一样)就足够了。

    更新code sandbox

    【讨论】:

      【解决方案3】:

      &lt;App /&gt; 组件中删除&lt;Login /&gt; 标签并将其添加到component 属性中,就像添加注册一样。您可以在“/”路径中查看登录页面。

      然后从登录组件中删除&lt;Link to="/"&gt; 标签,只保留&lt;Link to="/sign-up" &gt;。然后,当您点击登录页面中的链接时,您将被路由到注册组件。

      但是,要让它工作,你需要来自“react-router”的“历史”。从“react”导入“useHistory”并将history = useHistory()放入您的登录组件中(以及在注册组件中进行路由返回)并添加&lt;Link to="sign-up" onClick = {() =&gt; history.push('/sign-up')}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-27
        • 2018-12-07
        • 1970-01-01
        相关资源
        最近更新 更多