Search code examples
javascriptreactjsreduxredux-router

How to add push method from redux-router to actions


I'm trying to add push method from redux-router in actions. When I init action, push receiving an object in payload, but don`t change the page. enter image description here

actions/index.js

import { push } from 'redux-router';

export const actions = {};

actions.goTo = () => {
    return (dispatch) => {
        dispatch(push('/staff'));
    };
};

But if I init push not from the action, but straight in the component it works right.

MyComponent/index.js

import { push } from 'redux-router';
import { connect } from 'react-redux';

@connect((state) => ({}))
export default class MyComponent extends Component {

constructor (props) {
        super(props);
        this._goTo = ::this._goTo;
    }
    
_goTo (event) {
        event.preventDefault();
        const { dispatch } = this.props;
        dispatch(push('/staff'));
    }
    
    render () {
        return (
            <section>
              <button onClick = { this._goTo }>from component</button>
            </section>
        );
    }
}


Solution

  • Probably error while you compose your middlewares.

    Note: Router middleware should follow after Logger middleware.

    Like this one:

    compose(
        applyMiddleware(...middleware),
        reduxReactRouter({ createHistory })
    );