Redux-saga 从操作中获取数据 returns patternOrChannel 未定义

Redux-saga get data from action returns patternOrChannel is undefined

我需要将动态数据从我的屏幕发送到 action/reducer 并使用该数据从 API 获取数据,但是当我在 rootSaga 中屈服时我会收到这样的错误:

uncaught at check take(patternOrChannel): patternOrChannel is undefined
uncaught at rootSaga at rootSaga
at takeEvery
Error: take(patternOrChannel): patternOrChannel is undefined

屏幕代码:

import { checkUserLoginStatus, userSignin } from '../actions/user';

class PreCheckout extends PureComponent {
   handleLogin = () => {
     this.props.dispatch(userSignin(username, password));
   };
   render() { .......

操作:

const USER_SIGNIN = 'USER_SIGNIN';

export const userSignin = (username, password) => ({
   type: USER_SIGNIN,
   username,
   password,
});

减速器:

import {
  CHECK_USER_LOGIN_STATUS,
  USER_SIGNIN,
  USER_SIGNIN_RESULT,
  USER_SIGNIN_ERROR,
} from '../actions/user';

const initialState = {
  isLoggedIn: false,
  isFetching: false,
  information: {},
  error: null,
};

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case CHECK_USER_LOGIN_STATUS:
      return {
        ...state,
      };
    case USER_SIGNIN:
      return {
        ...state,
        isFetching: true,
      };
    case USER_SIGNIN_RESULT:
      return {
        ...state,
        isFetching: false,
        information: action.result,
      };
    case USER_SIGNIN_ERROR:
      return {
        ...state,
        isFetching: false,
        error: action.error,
      };

Redux 传奇:

import {
  USER_SIGNIN,
  USER_SIGNIN_RESULT,
  USER_SIGNIN_ERROR,
} from '../actions/user';

function* fetchUserInformation(action) {
  try {
    console.log('fetchUserInformation action: ', action);
    const response = yield call(login, action);
    yield put({
      type: USER_SIGNIN_RESULT,
      result: response.result,
    });
  } catch (e) {
    yield put({
      type: USER_SIGNIN_ERROR,
      error: e.message,
    });
  }
}

export default function* rootSaga() {
  yield takeEvery(USER_SIGNIN, fetchUserInformation);
}

可能 USER_SIGNIN 未定义。

正如我在评论中提到的。您只是忘记导出常量。

应该是

export const USER_SIGNIN = 'USER_SIGNIN';

const USER_SIGNIN = 'USER_SIGNIN';

...

export { USER_SIGNIN };

这些类型的错误可以通过 eslint 使用 eslint-plugin-import 并启用此 rule 来捕获。