React-router: RR V4 Programmatically Change Routes

Created on 22 Sep 2016  路  7Comments  路  Source: ReactTraining/react-router

Version

4.0.0

Test Case

Can't access router history to push state.

Steps to reproduce

Attempt to access programmatically the ability to change routing.

Expected Behavior

history.push (path) is what I would expect to push to a new route
alternatively history.replaceState (path) etc.

Actual Behavior

history is not available, confused as to if I need to import a library for this.

Most helpful comment

@timdorr so what's the correct way to redirect in V4 from redux-saga for example? There's no access to router context methods.

All 7 comments

It appears I can access the router methods I need through context, however I was under the impression that using context was an anti pattern and would be deprecated?

We can't prevent its use, only discourage it. You might be interested in react-history for a declarative means to issue history actions. It's pretty neat.

@timdorr so what's the correct way to redirect in V4 from redux-saga for example? There's no access to router context methods.

Any news on this? I need to be able to send the user to a specific URL after they submit a form, so I can't use <Link>.

I checked react-history but it's not really what I need. Are there plans to include withRouter in v4?

I think you want to use react-history and do something like history.push ('/route');

As best as possible I try to use either the Link or Redirect component. Accessing react-history works, but could reduce the reusability of the action.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ackvf picture ackvf  路  3Comments

nicolashery picture nicolashery  路  3Comments

Waquo picture Waquo  路  3Comments

imWildCat picture imWildCat  路  3Comments

winkler1 picture winkler1  路  3Comments