Can PERK
Can PERK

Reputation: 630

React Router v4 navigation programmatically

When I use react router with Redirect, Link or NavLink eveything is fine. But I need a function that navigates me to a route programmatically.

That is what I want :

if(a == 1){
    this.context.history.push("/")
}
else {
    this.context.history.push("/home");
}

As you see context history is not exists in react router v4 any more. I cannot write withroute in js code for navigation. Can anyone say me how to navigate directly in js code?

Thanks

Upvotes: 5

Views: 3507

Answers (1)

Moe
Moe

Reputation: 3733

The router will add a history to your props, you can use it like this:

this.props.history.push('/mypath')

Check this for more info: Navigating Programatically in React-Router v4

Upvotes: 9

Related Questions