【问题标题】:How to check if redux actionReducer axios request is successful or not如何检查redux actionReducer axios请求是否成功
【发布时间】:2023-02-11 20:57:39
【问题描述】:

我正在使用 OTP 创建页面登录,所以首先我发送了登录请求。如果成功,那么我的模型将打开以输入 otp。 但在我的情况下,模型将始终打开,因为我无法弄清楚如何检查我的请求是否成功

我可以管理状态,但我不想为这个简单的任务管理状态。我只希望请求在 action reducer 中成功与否。这肯定会轻松解决我的问题。

    const handleSubmit = async (event) => {
    event.preventDefault();
    const data = new FormData(event.currentTarget);

    let email= data.get('email');
    let password= data.get('password');

    await props.otpRequest(email,password);
    handleOTPModelOpen();
  };

我只想检查 otp 请求是否成功。就像我们在 action reducer 中签入 axios 请求一样。

所有请求都成功没有错误

【问题讨论】:

    标签: reactjs redux redux-toolkit redux-thunk


    【解决方案1】:

    对于大多数库来说,处理这个问题的一种好方法是 try / catch 块。

    你可以这样做:

    try { 
      await props.otpRequest(email,password);
      handleOTPModelOpen();
    } catch (e) {
      // handle error here
      console.log(e);
      handleOTPFailed();
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 1970-01-01
      • 2018-01-15
      • 2019-11-18
      • 1970-01-01
      • 2020-08-23
      • 2021-01-28
      • 2015-08-14
      • 1970-01-01
      相关资源
      最近更新 更多