Redux-saga从动作中获取数据返回patternOrChannel未定义

时间:2018-01-22 09:45:40

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

我需要将动态数据从我的屏幕发送到action / reducer并使用该数据从API获取数据,但是当我在 rootSaga 中产生时,我会得到如下错误:

  

检查时未捕获(patternOrChannel):patternOrChannel未定义
   在rootSaga的rootSaga没有被捕    at takeEvery
   错误:take(patternOrChannel):patternOrChannel未定义

屏幕代码:

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,
      };

终极版-佐贺:

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);
}

2 个答案:

答案 0 :(得分:4)

正如我在评论中提到的那样。你只是忘了导出常量。

应该是

export const USER_SIGNIN = 'USER_SIGNIN';

或者

const USER_SIGNIN = 'USER_SIGNIN';

...

export { USER_SIGNIN };

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

答案 1 :(得分:3)

可能USER_SIGNIN未定义。