React router redirect after action redux

ximet picture ximet · Feb 29, 2016 · Viewed 93.9k times · Source

I'm using react-redux and standard react-routing. I need redirect after definite action.

For example: I have registration a few steps. And after action:

function registerStep1Success(object) {
    return {
        type: REGISTER_STEP1_SUCCESS,
        status: object.status
   };
}

I want him redirect to page with registrationStep2. How to do it?

p.s. In history browser '/registrationStep2' has never been. This page appears only after successful result registrationStep1 page.

Answer

Dan Abramov picture Dan Abramov · Mar 1, 2016

With React Router 2+, wherever you dispatch the action, you can call browserHistory.push() (or hashHistory.push() if that’s what you use):

import { browserHistory } from 'react-router'

// ...
this.props.dispatch(registerStep1Success())
browserHistory.push('/registrationStep2')

You can do this from async action creators too if that is what you use.