如何在 react-router-dom Link 中传递参数? [英] How to pass parameters in react-router-dom Link?

查看:34
本文介绍了如何在 react-router-dom Link 中传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have links to components-pages, for example:

<Link to="/"> Home <Link/> 

在道具中我有一个参数,例如 userId.我需要在链接中传递它.我知道如何在 onCLick() 中传递参数:

In props I have a parameter, for example userId. I need to pass it in Link. I know how to pass params in onCLick():

this.props.router.push({
  pathname: '/',
  state: {
    userId: 7
  }
})

我如何理解 inink 可以执行 onClick,但是否可以在其中传递某种参数?不创建onClick?

How I understand in ink can be performed onClick, but may be it's possible to pass somehow params in it? Without creating onClick?

推荐答案

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

Yes, you can pass an object to the "to" prop like this.

 <Link to={{
  pathname: '/courses',
  search: '?sort=name',
  hash: '#the-hash',
  state: { fromDashboard: true }
 }}/>

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

Check the documentation for more. https://reacttraining.com/react-router/web/api/Link.

这篇关于如何在 react-router-dom Link 中传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆