【问题标题】:Why saga effect not getting called?为什么传奇效应没有被调用?
【发布时间】:2019-02-22 21:40:02
【问题描述】:

我正在尝试在我的应用上添加多个动作效果。但我想我错过了一些东西。没有调用效果函数。我希望命名有问题吗?理想情况下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;

【问题讨论】:

  • 嗨,Subhendu,您是否已将 saga 中间件与您的 Redux 存储连接?
  • 请同时添加您的操作调度语句。您正在调度该操作吗?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多