0

How does one do routing with params passed into it through function calls in react-router?

For example I have this Link that when click routes to a different url with parameters passed in:

<Link key={category.name} to={`/category/${category.name}`} params={{category: category.name}} >{category.name}</Link>

Suppose I want to simulate what happens when one clicks on the above link through a function call, how does one go about doing this?

clickHandler(){
//What function should be called to do the routing through a function call?

}
preston
  • 3,721
  • 6
  • 46
  • 78
  • 1
    Possible duplicate of [React-router: How to manually invoke Link?](https://stackoverflow.com/questions/29244731/react-router-how-to-manually-invoke-link) – Héctor Nov 09 '17 at 15:13

1 Answers1

1

Use the history object.

If the component which renders the Link is rendered by the router, then you have in this.props a history property you can use.

clickHandler(){
    const {
        history
        category // ??
    } = this.props
    history.push(`/category/${category.name}`);
}
ChrisR
  • 3,922
  • 1
  • 14
  • 24