6

I have links to components-pages, for example:

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

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
  }
})

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

Sam Fisher
  • 746
  • 2
  • 10
  • 27

1 Answers1

15

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 }
 }}/>

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

Kris
  • 166
  • 2
  • 5
  • To get the data passed to the `to` object, you need to use the `useLocation` hook on the next screen. `let location = useLocation();` – Adrian Bartyczak Feb 15 '22 at 06:07