【问题标题】:react router v4 route changed but did not render the correct component反应路由器 v4 路由已更改但未呈现正确的组件
【发布时间】:2018-01-01 16:11:37
【问题描述】:

我有2层路线,第一层是这样的

<BrowserRouter>
  <div>
    <AuthRoute></AuthRoute>
    <Switch>
      <Route path='/dashboard' component={Dashboard} />
    </Switch>
  </div>
</BrowserRouter>

在 AuthRoute 中我对 componentDidMount 进行了重定向 this.props.history.replace(/dashboard/redirected)

问题出在仪表板/索引中,这是我的第二级路由配置

<BrowserRouter>
  <Switch>
    <Route exact path='/dashboard' component={()=><h1>dashboard index</h1>} />
    <Route exact path='/dashboard/somewhere' component={()=><h1>somewhere</h1>} />
    <Route exact path='/dashboard/redirected' component={() => <h1>redirected</h1>} />
  </Switch>
</BrowserRouter>

路由更改为/dashboard/redirect,但如果您刷新说/dashboard/somewhere/dashboard/,组件未呈现正确的路由

你可以在我设置https://codesandbox.io/s/v0v4qok38l的demo中更清楚地看到问题

【问题讨论】:

    标签: javascript reactjs react-router


    【解决方案1】:

    您在应用程序中只需要一个&lt;BrowserRouter&gt; - 从dashboard/index.js 中删除它即可解决您的问题。

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 1970-01-01
      • 2017-12-06
      • 2017-09-16
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多