【问题标题】:How to use React-router render()如何使用 React-router 渲染()
【发布时间】:2019-04-09 15:55:24
【问题描述】:

我已阅读onEnter not called in React-Router 但我不知道如何解决我的问题。 我只想检查登录状态并重定向

index.js

import indexRoutes from "routes/index.jsx";

    ReactDOM.render(
      <Router history={hist}>
        <Switch>
          {indexRoutes.map((prop, key) => {
            return <Route path={prop.path} component={prop.component} key={key} />;
          })}
        </Switch>
      </Router>,
      document.getElementById("root")
    );

如何将我的代码更改为此?

<Route exact path="/home" render={() => (
  isLoggedIn() ? (
    <Redirect to="/front"/>
  ) : (
    <Home />
  )
)}/>

【问题讨论】:

  • 您能更准确地描述您的问题吗?

标签: reactjs react-router-v4


【解决方案1】:

您不应该在 index.js 中定义它。

我建议在你的 index.js 中添加另一个类引用并将逻辑放在里面。

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2018-01-07
    • 2020-03-12
    • 1970-01-01
    • 2021-01-11
    • 2020-12-18
    • 2016-02-22
    • 2019-01-02
    • 2016-06-13
    相关资源
    最近更新 更多