为什么传奇效果没有被调用?

时间:2019-02-22 21:40:02

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

我正在尝试在我的应用上添加多种动作效果。但是我想我缺少了一些东西。效果函数没有被调用。我希望命名有问题吗?理想情况下,yield all([应该可以解决问题吗? 帮我弄清楚。

action.js

import {
  DEFAULT_ACTION,
  POST_ADMIN_LOGIN_REQUEST,
  POST_ADMIN_LOGIN_REQUEST_SUCCESS,
  POST_ADMIN_LOGIN_REQUEST_ERROR,
  POST_ADMIN_USER_DETAILS,
} from './constants';

import { setCookie } from '../../utils/helpers';

...... //rest of my actions

export function postAdminUserDetails(token) {
  console.log(token);
  // I see token in the console
  return {
    type: POST_ADMIN_USER_DETAILS,
    token,
  };
}

constants.js

export const POST_ADMIN_USER_DETAILS = 'app/AdminLogin/POST_ADMIN_USER_DETAILS';

saga.js

import { takeLatest, call, put, all } from 'redux-saga/effects';
import request from 'utils/request';

import { POST_ADMIN_LOGIN_REQUEST, POST_ADMIN_USER_DETAILS } from './constants';

console.log(POST_ADMIN_USER_DETAILS)

import {
  postAdminLoginRequestSuccess,
  postAdminLoginRequestError,
} from './actions';

export function* triggerLogin({ params }) {
  const requestURL = new URL(`${API}/login`);

  ...... //rest of the logis, which is working
}

export function* triggerGetUser({ token }) {
  const requestURL = new URL(`${API}/profile`);

  console.log('callllled');
  // this is not even getting called :(
}

export default function* defaultSaga() {
  console.log('POST_ADMIN_USER_DETAILS')
  yield all([
    takeLatest(POST_ADMIN_LOGIN_REQUEST, triggerLogin),
    takeLatest(POST_ADMIN_USER_DETAILS, triggerGetUser),
  ]);
}

reducer.js

import { fromJS } from 'immutable';
import {
  DEFAULT_ACTION,
  POST_ADMIN_LOGIN_REQUEST,
  POST_ADMIN_LOGIN_REQUEST_SUCCESS,
  POST_ADMIN_LOGIN_REQUEST_ERROR,
  POST_ADMIN_USER_DETAILS,
} from './constants';

export const initialState = fromJS({
  isLogin: false,
  hasError: false,
  loginResponse: null,
});

function adminLoginReducer(state = initialState, action) {
  switch (action.type) {
    case DEFAULT_ACTION:
      return state;
    case POST_ADMIN_LOGIN_REQUEST:
      return state
        .set('isLogin', true)
        .set('hasError', false)
        .set('errorMessage', null)
        .set('loginResponse', null);
    case POST_ADMIN_LOGIN_REQUEST_SUCCESS:
      return state
        .set('isLogin', false)
        .set('hasError', false)
        .set('loginResponse', action.response);
    case POST_ADMIN_LOGIN_REQUEST_ERROR:
      return state
        .set('isLogin', false)
        .set('hasError', true)
        .set('errorMessage', action.errorMessage);
    case POST_ADMIN_USER_DETAILS:
      return state;
    default:
      return state;
  }
}

export default adminLoginReducer;

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,通过更改文件夹的路径解决了它。因此,请尝试相同的文件或将saga文件与组件文件保持在同一文件夹级别。