React-Router-Redux / Sagas / Push Undefined

时间:2018-05-16 03:42:39

标签: reactjs redux-saga react-router-redux

当我尝试使用react-router-redux 5.0.0-alpha.9时,我发现在Saga中发生了错误。

import { push } from 'react-router-redux';
import ReactOnRails from 'react-on-rails';
import { call, put, takeLatest } from 'redux-saga/effects';
import { LOGIN, loginSuccess, loginFailure } from '../users/actions';
import loginService from './services';

function* loginSaga(action) {
  const { loginValues } = action;

  try {
    const loginData = yield call(loginService, loginValues);
    const store = ReactOnRails.getStore('appStore', false);
    yield put(loginSuccess(loginData));
    yield call(store.dispatch(push('/')));
  } catch (error) {
    yield put(loginFailure(error.response.data.error));
  }
}

function* loginWatcherSaga() {
  yield takeLatest(LOGIN, loginSaga);
}

export default loginWatcherSaga;

登录成功时会出现问题。它将成功调用'loginSuccess'动作,然后转到yield call(store.dispatch(push('/')));行,但它会轰炸并跳转到捕获。这是错误消息:

utils.js:225 uncaught at check call: argument fn is undefined
log @ utils.js:225
check @ utils.js:29
getFnCallDesc @ io.js:88
call @ io.js:113
loginSaga @ sagas.js:14
next @ proc.js:311
currCb @ proc.js:388
(anonymous) @ proc.js:499
exec @ scheduler.js:19
flush @ scheduler.js:60
asap @ scheduler.js:33
runPutEffect @ proc.js:486
runEffect @ proc.js:435
next @ proc.js:315
currCb @ proc.js:388
Promise.then (async)
resolvePromise @ proc.js:450
runCallEffect @ proc.js:517
runEffect @ proc.js:435
next @ proc.js:315
proc @ proc.js:270
runForkEffect @ proc.js:554
runEffect @ proc.js:435
next @ proc.js:315
currCb @ proc.js:388
takeCb @ proc.js:466
put @ channel.js:73
(anonymous) @ channel.js:161
(anonymous) @ channel.js:186
exec @ scheduler.js:19
flush @ scheduler.js:60
asap @ scheduler.js:33
(anonymous) @ channel.js:185
emit @ channel.js:26
(anonymous) @ middleware.js:67
login @ LoginForm.jsx:131
handleSubmit @ LoginForm.jsx:31
callCallback @ react-dom.development.js:542

基本上它是在这条线上轰炸:

yield call(store.dispatch(push('/')));

当我调试它时,push是未定义的。我没理由。我有这个确切的代码在一个不同的传奇工作:

function* setPasswordSaga(action) {
  try {
    const response = yield call(setPasswordService, action.passwordValues);
    const store = ReactOnRails.getStore('appStore', false);
    yield put(setPasswordSuccess(response));
    yield call(store.dispatch(push('/')));
  } catch (error) {
    yield put(setPasswordFailure(error.response.data.user));
  }
}

它改变路由就好了。我无法弄清楚为什么一个有效而另一个没有。有任何想法吗?

有更好的方法吗?我试图在动作中进行路由,但是在呈现之前状态没有在组件中更新所以我必须以某种方式执行服务,更新状态,然后更新路由。对最好的方法感到困惑。

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用yield put(push(YOUR_ROUTE))