【问题标题】:React-Router-Redux / Sagas / Push UndefinedReact-Router-Redux / Sagas / Push Undefined
【发布时间】:2018-10-25 23:51:20
【问题描述】:

我在尝试使用 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));
  }
}

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

有没有更好的方法来做到这一点?我试图在操作中进行路由,但是在渲染之前组件中的状态没有更新,所以我必须以某种方式执行服务,更新状态,然后更新路由。对执行此操作的最佳方法感到困惑。

谢谢!

【问题讨论】:

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


    【解决方案1】:

    试试yield put(push(YOUR_ROUTE))

    【讨论】:

      猜你喜欢
      • 2022-12-10
      • 2019-07-12
      • 1970-01-01
      • 2017-07-12
      • 2017-09-25
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多