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?

}
1

1 Answer 1

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}`);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.