【问题标题】:Default root in react-router v4 in reactreact-router v4中的默认root
【发布时间】:2018-06-13 17:52:21
【问题描述】:

我曾经使用早期版本的 react-router 默认根可以使用 IndexRoute 设置,但现在在 react-router v4 中我迷路了。

<Switch>
  <Redirect exact from='/' to='/dashboard' />
  <Route exact path='/dashboard' component={Dashboard} />
</Switch>

上面的代码试过了,好像不行,对使用重定向没有影响。

【问题讨论】:

标签: reactjs react-router


【解决方案1】:

带有路径参数的 Route 将始终匹配。您需要正确配置您的路线

<Switch>
  {/* Your other Routes */}
  <Route component={Dashboard} />
</Switch>

在 switch 的帮助下,第一个匹配的 Route 被渲染,因此您需要正确排序您的 Route 以便渲染正确的 Route

【讨论】:

    【解决方案2】:

    你需要使用 withRouter 像这样: export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App))

    【讨论】:

    • 您的回答措辞不当。答案应该提供明确的解释。不应使用“也许”之类的词。请在发布前阅读 SO 指南。
    猜你喜欢
    • 2019-02-25
    • 2018-08-08
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 2017-10-26
    • 2017-12-29
    • 2018-01-07
    相关资源
    最近更新 更多