反应 - "减速器可能不会发出动作." 导航到路线后

use*_*293 6 reactjs react-router

我正在使用

hashHistory.push('/home')
Run Code Online (Sandbox Code Playgroud)

导航到我的应用程序上的某些路线.但是,每当我使用它时,我都会得到它

Reducers may not dispatch actions.
Run Code Online (Sandbox Code Playgroud)

如何在不收到此错误的情况下正确导航到某条路线?

T M*_*ell 3

假设你正在使用 Redux,你可以使用 thunk 中间件和react-router-redux

https://github.com/gaearon/redux-thunk

https://github.com/reactjs/react-router-redux

这将允许您调度一个操作,减少它,然后再调度另一个操作

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

const someAction = (somePayload) => (dispatch, getState) => {
  dispatch({
    type: 'SOME_ACTION',
    payload: { 
      somePayload
    }
  });

  // Get the updated state from the store and navigate
  let { someVar } = getState();
  if (someVar === 'some value') {
    dispatch(push('/home'));
  }
};


// Call like so
store.dispatch(someAction())
Run Code Online (Sandbox Code Playgroud)

如果您不使用 redux 或者不想走这条路,请确保您没有在减速器内或该操作周期的一部分中调度操作