React router, codes after history.push run?

Viewed 1358

Say on your clickHandler

you do

// url change will cause this component to be dismounted and a new component will mount
history.push(url)

// do some more work 
dispatch(someMoreWork())

What happens to the code after the push call? is the code guaranteed to run?

1 Answers

history navigation is done only after the sync action within the event queue in javascript are executed.

In your case, javascript will executed all the things within the function where you call history.push and will then navigate to the new Route and it will be a predictable behaviour

You can simply validate the above by executing the dispatch action and then accessing the value in the rendered component.

Check this demo that demonstrates the above behavior by dispatching an action after history.push and accessing the resultant redux state in the rendered route

Related