2

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>
        );
    }
}
mikilka
  • 23
  • 3

2 Answers2

0

So, push() may only work from inside a connected component. If you want to use it from your actions/index.js file, you can pass it along as a param and call it there, without having to import it. You can have something like:

export const actions = {};

actions.goTo = (route, push) => {
    return () => {
        push(`/${route}`);
    };
};

and call it as actions.goTo('staff', push) from your component.

Kelvin De Moya
  • 5,118
  • 1
  • 17
  • 16
  • I tried this I've received this error in replaceRoutesMiddleware.js `Uncaught TypeError: Cannot read property 'type' of undefined` – mikilka Feb 17 '17 at 13:42
0

Probably error while you compose your middlewares.

Note: Router middleware should follow after Logger middleware.

Like this one:

compose(
    applyMiddleware(...middleware),
    reduxReactRouter({ createHistory })
);
aprisniak
  • 302
  • 5
  • 15