【问题标题】:React - Wrong component rendering after calling history.pushReact - 调用history.push后错误的组件渲染
【发布时间】:2019-11-11 00:02:12
【问题描述】:

我有一个非常基本的应用程序,只有几个页面:主页、登录和仪表板。目标是让用户成功登录并导航到仪表板。目前,在我单击登录表单后,浏览器中的url/path 更新为/dashboard,但我仍然看到auth 组件。如果我在浏览器中进行刷新,我只能看到仪表板组件。我做错了什么?

https://stackblitz.com/edit/react-scqvhp

"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0"

App.js

export default function App() {
  return (
    <Router>
      <div className="App">
          <Switch>
            <Route exact path="/">
              <Home />
            </Route>
            <Route path="/auth">
              <Auth />
            </Route>
            <Route path="/dashboard">
              <Dashboard />
            </Route>
          </Switch>
      </div>
    </Router>
  );
}

Auth.js

export default function Auth() {
  let match = useRouteMatch();

  return (
    <div className="d-flex align-items-center bg-auth border-top border-top-2 border-primary vh-100">
      <div className="container">
        <div className="row justify-content-center">
          <div className="col-12 col-md-5 col-xl-4 my-5">
            <p>Auth</p>
            <Router>
              <Route path={`${match.path}/login`}><Login /></Route>
            </Router>
          </div>
        </div>
      </div>
    </div>
  );
};

登录.js

export default function Login() {
  let history = useHistory();

  const submit = (e) => {
    e.preventDefault();

    history.push('/dashboard');
  };

  return (
    <div>
      <form onSubmit={submit}  data-op-form-id="0">
        <div className="form-group">
          <label>Email Address</label>
          <input type="email" className="form-control" placeholder="name@address.com" />
        </div>
        <div className="form-group">
          <div className="row">
            <div className="col">
              <label>Password</label>
            </div>
            <div className="col-auto">
              <a href="#" className="form-text small text-muted">
                Forgot password?
              </a>
            </div>
          </div>
          <div className="input-group input-group-merge">
            <input type="password" className="form-control form-control-appended" placeholder="Enter your password" />
            <div className="input-group-append">
                  <span className="input-group-text">
                    <Eye/>
                  </span>
            </div>
          </div>
        </div>
        <button className="btn btn-lg btn-block btn-primary mb-3">Sign in</button>
        <div className="text-center">
          <small className="text-muted text-center">Don't have an account yet? <a href="#">Sign up</a>.</small>
        </div>
      </form>
    </div>
  );
};

仪表板.js

export default function Dashboard() {
  return (
    <div>
      <Sidebar/>
      <p>Dashboard</p>
    </div>
  );
};

【问题讨论】:

    标签: javascript reactjs react-router react-router-v4


    【解决方案1】:

    删除Auth中的嵌套Router,它将与URL路由冲突,即没有为嵌套路由器声明/dashboard路由,因此它呈现没有子视图的Auth视图

    export default function Auth() {
      let match = useRouteMatch();
    
      return (
        <div className="d-flex align-items-center bg-auth border-top border-top-2 border-primary vh-100">
          <div className="container">
            <div className="row justify-content-center">
              <div className="col-12 col-md-5 col-xl-4 my-5">
                <p>Auth</p>
                <Route path={`${match.path}/login`}><Login /></Route>
              </div>
            </div>
          </div>
        </div>
      );
    };
    

    每个应用程序只需要一个Router 实例,请参阅docs on nesting

    【讨论】:

      【解决方案2】:

      您的路线设置不正确。这是live demo

        <Router>
          <div className="App">
              <Switch>
                <Route exact path="/">
                  <Home />
                </Route>
                <Route exact path="/auth">
                  <Auth />
                </Route>
                <Route exact path="/auth/login">
                  <Login />
                </Route>
                <Route path="/dashboard">
                  <Dashboard />
                </Route>
              </Switch>
          </div>
      </Router>
      

      另外删除路由器从auth.js

      <Router>
         <Route path={`${match.path}/login`}><Login /></Route>
      </Router>
      

      【讨论】:

      • 很高兴您解决了这个问题,但如果您更改答案以解释您更改的什么以及为什么它会修复,那就更好了问题
      • 就像我说的路由设置不正确OP只需要在这种情况下添加正确的路由 /auth/login 并且不需要嵌套路由器
      猜你喜欢
      • 2018-11-28
      • 2019-08-03
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 2020-11-30
      相关资源
      最近更新 更多