【问题标题】:Passing down location props to a components that is not routed将位置道具传递给未路由的组件
【发布时间】:2020-04-25 23:43:01
【问题描述】:

大家好,我想将位置道具传递给导航栏, 但是我不能这样做,因为它不是路由组件, 它总是在那里,并且位置返回未定义

有什么建议吗?

代码:

  <Router>
        <NavBar  location={this.props.location}></NavBar>
    <Switch>
      <div className="wrapper">
        <Route exact  path="/" component={Home} />
        <Route path="/about" component={About} />
        <footer>Footer</footer>
      </div>
    </Switch>
  </Router>

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    如果你正在渲染的组件没有 this.props.location,那么它就没有位置向下传递给 NavBar。

    不过,如果它需要访问该位置而不是 Route 本身,我建议在您的 NavBar 组件中使用 withRouteruseLocation。注意,useLocation 只能在钩子组件中使用

    【讨论】:

      猜你喜欢
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 2015-09-01
      • 2020-03-26
      • 2020-04-03
      • 2018-05-19
      • 1970-01-01
      相关资源
      最近更新 更多