acdlite / redux-router

Redux bindings for React Router – keep your router state inside your Redux store
MIT License
2.3k stars 200 forks source link

`store.dispatch(actionCreator())` in history listener makes action creator work weirdly #157

Closed sairion closed 8 years ago

sairion commented 8 years ago
// history.js
history.listen((error, nextState) => {
     store.dispatch(aC()); // making this 'setTimeout(() => store.dispatch(aC()), 0)' solves problem
});

// actionCreator.js
export function aC () {
    return dispatch => {
        dispatch(aC1()); // => suddenly call this function body again, kind of 'recursively'
        dispatch(aC2());
    }
}

I was debugging this and found this behavior. Am I supposed to leave store.dispatch out of history listener?

Scarysize commented 8 years ago

Yes this may lead to some trouble. We are trying to keep history and the store in sync. Which uses a combination of history.listenand store.subscribe. So dispatching something in your history.listen function may very well screw some things up. If you want to do some dispatching work on history/route changes you may have to fallback to using React lifecycle methods or hooking in your own middleware to listen for redux-router actions.