javascript - 为什么传奇效应没有被调用?

标签 javascript reactjs react-native redux redux-saga

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

常量.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;

最佳答案

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

关于javascript - 为什么传奇效应没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54835561/

相关文章:

javascript - 在 React Native 中垂直对齐文本

ios - 有没有办法从 React Native 中的 chrome 控制台重新加载设备,这样我就不必不断地摇动它?

javascript - $(...).DataTable(...).rows 不是函数

javascript - Canvas 点对点用户线

javascript - 外部网络 : Uncaught TypeError: Cannot read property 'type' of null

javascript - 提前退出 useEffect 钩子(Hook)导致 'Error: too many re-renders'

javascript - 将用户输入 onchange 传递给父组件的 React Javascript 问题

reactjs - 如何在 native 基础选项卡中使用刷新控件?

javascript - React-navigation 在调试时工作正常但在 Release模式下不工作

jquery - 使 jQuery ui 对话框不起作用?