如何在react-router-dom Link中传递参数?

Sam*_*her 1 reactjs react-router

我有组件页面的链接,例如:

<Link to="/"> Home <Link/> 
Run Code Online (Sandbox Code Playgroud)

在props中我有一个参数,例如userId.我需要在Link中传递它.我知道如何通过params onCLick():

this.props.router.push({
  pathname: '/',
  state: {
    userId: 7
  }
})
Run Code Online (Sandbox Code Playgroud)

我可以如何理解墨水onClick,但也许可以通过某种方式传递它?没有创造onClick

小智 9

是的,您可以像这样将对象传递给"to"道具.

 <Link to={{
  pathname: '/courses',
  search: '?sort=name',
  hash: '#the-hash',
  state: { fromDashboard: true }
 }}/>
Run Code Online (Sandbox Code Playgroud)

查看文档了解更多信息.https://reacttraining.com/react-router/web/api/Link.


归档时间:

查看次数:

8486 次

最近记录:

7 年,8 月 前