账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react ,使用this.props.dispatch(actions.update) 发起了一个请求,但是没有执行
    40
    0

    问题描述

    react redux ,使用this.props.dispatch(actions.update) 发起了一个请求,但是没有执行

    问题出现的环境背景及自己尝试过哪些方法

    界面是一个tab 切换,点击的时候在组件 componentWillMount的时候 调用了
    this.props.dispatch(actions.fetchALLlevels)

    控制台能打印出来 这个action,但是请求没有发送

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)
    import { ofType } from 'redux-observable';
    import { of } from 'rxjs';
    import { switchMap, map, catchError } from 'rxjs/operators';
    import { ajax } from 'rxjs/ajax';
    
    import * as actions from '../actions/level';
    
    export const fetchAllLevels = action$ =>
    
      action$.pipe(
        ofType(actions.fetchAllLevels),
        switchMap(() =>
          ajax({
            url: '/api/v1/rank/get',
            method: 'GET',
            headers: {
              Authorization: localStorage.getItem('token'),
            },
          }).pipe(
            map(data => {
              actions.fetchAllLevelsSuccess(data.response)}),
            catchError(err => {
              toastr.error('获取职级失败');
              return of(actions.fetchAllLevelsError(err));
            }),
          ),
        ),
      );
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • щǒ就是这麽↘拽 普通会员 1楼

      在React中,this.props.dispatch函数是一个用于发送事件到父组件的方法,它会在组件的状态改变时触发。如果this.props.dispatch函数没有被调用,那么它就不会触发任何事件。

      这可能是因为你没有正确地调用this.props.dispatch函数。例如,如果你在一个方法中调用了this.props.dispatch,然后在这个方法的最后调用了另一个方法,那么这个第二个方法就不会被调用。

      在这种情况下,你可以尝试在this.props.dispatch函数内部添加一些检查,以确保this.props.dispatch函数是在正确的时刻被调用的。

      例如,你可以这样写:

      jsx this.props.dispatch(async () => { // 在这里进行你的操作 });

      在这个例子中,async () =>是一个异步函数,它会在组件的状态改变时触发this.props.dispatch函数。

      另外,你还可以使用useEffect Hook来执行this.props.dispatch函数。这个Hook会在组件的状态改变时触发this.props.dispatch函数。例如:

      ```jsx import { useEffect } from 'react';

      function App() { useEffect(() => { this.props.dispatch(async () => { // 在这里进行你的操作 }); }, [this.props]);

      return (

      {/ your other code /}
      ); } ```

      在这个例子中,useEffect Hook会在组件的状态改变时触发this.props.dispatch函数。

    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部