【问题标题】:React Router, Getting location in nested childReact Router,在嵌套子节点中获取位置
【发布时间】:2017-01-28 02:05:18
【问题描述】:

使用withRouter HOC https://github.com/ReactTraining/react-router/blob/master/upgrade-guides/v2.4.0.md,有没有办法得到location的信息?

还是我们仍然需要使用我们自己的上下文提供者和 hoc 来访问它?

【问题讨论】:

    标签: react-router


    【解决方案1】:

    locationwithRouter 注入到您的组件中的道具之一。

    const withRouter(({ location }) => (
      <div>You are here: {location.pathname}</div>
    ))
    

    【讨论】:

    • 哦,真的吗?我以为 withRouter 只会给我 this.props.router,谢谢!
    猜你喜欢
    • 2018-09-16
    • 2017-09-03
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多