【问题标题】:How to access id passed in Route inside Component? [duplicate]如何访问组件内部路由中传递的id? [复制]
【发布时间】:2018-12-01 08:44:04
【问题描述】:

我已经配置了这条路线。

<Router history={history}>
      <div>
         <NavBar />
     <Route path='/home' exact strict component={HomePageContainer} />
     <Route  path='/edit/:id' component={EditSchool}  />
       </div>
        </Router>

我想访问动态传递给组件 EditSchool 的 id 的值。 这是我的链接标签

 <Link to={'edit/'+schools.id }> {schools.school_name}</Link>

这是我的 URL 的样子:

/edit/12

但是如何在组件内部访问值 12?

this.props.route

返回未定义。

我应该将一个 prop 传递给路由然后访问它吗?

请有人帮忙。

【问题讨论】:

  • console.log(this.props)。你看到了什么?
  • 是的,找到了。我正在获取 json 数据。
  • @FrankerZ this.props.match.params.id 是我访问它所需要的

标签: reactjs react-native react-redux react-router


【解决方案1】:

您可以使用this.props.match.params.id 访问它。

【讨论】:

  • 是的,它现在可以工作了,谢谢 Alex :)
猜你喜欢
  • 2019-12-08
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
相关资源
最近更新 更多