Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dispatch actions on location change with react router v4

I am using react-router-4.0.0-beta.4 with redux, I am trying to figure out the best way to dispatch actions on route change. I see that a few versions ago there was an API/event hook for this here. But I see that it is now gone. Trying to do the modern equivalent of what redux-router did.

like image 207
Lutando Avatar asked Feb 27 '17 05:02

Lutando


1 Answers

Seems like the best way to dispatch an action is to do so at component's componentWillMount(), componentDidMount() or componentWillUnmount() methods.

IGNORE WHATS BELOW

There is onEnter prop. Like here comet-frontend

onEnter(nextState, replace, callback?)

Called when a route is about to be entered. It provides the next router state and a function to redirect to another path. this will be the route instance that triggered the hook. https://github.com/ReactTraining/react-router/blob/master/docs/API.md#onenternextstate-replace-callback

BUT, there still is onChange prop, doing exactly what you described. Maybe tell us what exactly is your issue. Because seems like you're doing something wrong.

like image 135
Patryk Cieszkowski Avatar answered Oct 06 '22 00:10

Patryk Cieszkowski