【问题标题】:Why are the react-router component props not including history?为什么 react-router 组件道具不包括历史?
【发布时间】:2017-08-10 06:22:47
【问题描述】:

我喜欢 v4,但在 4.1.2 中,这在使用浏览器路由器时总是让我感到困惑:

使用 Route 组件中的组件,我传入了这些 props:{computedMatch, location, path},尽管 documentation 告诉我期待 {match, location, history},这是我使用哈希路由器得到的。

要获取传入的历史记录,我必须使用 withRouter 包装器,这感觉非常笨拙,因为相关组件是 Route 组件的组件道具。

文档对我来说听起来不错。这是一个错误吗?

【问题讨论】:

    标签: react-router react-router-v4


    【解决方案1】:

    如果你使用Route as,你可以访问{match, location, history}

    <Route path="/" component={myComponent} 
    

    在上面的代码中,您将在 myComponent 中访问匹配位置和历史记录。

    否则你必须使用 withRouter

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2019-02-06
      • 2017-08-08
      • 2017-05-22
      • 2019-05-09
      • 2019-10-11
      • 2018-01-26
      • 2016-08-21
      • 2017-08-01
      相关资源
      最近更新 更多