【问题标题】:Invoking an async action creator helper with redux-thunk使用 redux-thunk 调用异步操作创建器助手
【发布时间】:2020-07-18 00:54:58
【问题描述】:

我正在尝试为两个异步操作创建者调用辅助方法,但我不确定为什么我不能这样做。分配给signUpOrSignIn 的函数应该被signUpsignIn 调用。这些都在同一个文件中。我认为这个问题涉及我的函数声明或async dispatch 的处理。非常感谢您的反馈。谢谢!

import axios from "axios";
import * as types from "./types";

const signUpOrSignIn = (path, email, password, history) => async dispatch => {
  try {
    const res = await axios.post(path, { email, password });
    history.push("/dashboard");
    dispatch({ type: types.DID_SIGN_IN, payload: res });
  } catch (err) {
    dispatch({ type: types.DID_SIGN_IN, payload: err.response });
  }
};

export const signUp = (email, password, history) => async dispatch => {
  signUpOrSignIn("/users", email, password, history);
};

export const signIn = (email, password, history) => async dispatch => {
  signUpOrSignIn("/users/login", email, password, history);
};

【问题讨论】:

  • 你在哪里调用signInsignup
  • 在 React 组件内的处理程序中。我知道signInsignUp 正在被调用,但都没有调用signUpOrSignIn
  • 你可以尝试在两个函数声明中都使用return signUpOrSignIn("/users", email, password, history);。您在箭头函数中使用{} 语法,但没有返回任何内容。
  • 刚刚尝试过,没有运气。
  • 您可以尝试从signUpsignIn 中删除async dispatch。他们都只是返回一个函数。无论如何,您都没有在其中使用dispatch。我认为这就是为什么signUporSignIn 没有被调用。如果您不想删除它,请尝试像这样调用它们 signUp(params)() signIn(params)()

标签: javascript axios redux-thunk


【解决方案1】:

因为signUpsignIn 打算只返回signUpOrSignIn

您可以删除async dispatch

像这样:

export const signUp = (email, password, history) => signUpOrSignIn("/users", email, password, history);

export const signIn = (email, password, history) => signUpOrSignIn("/users/login", email, password, history);

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 2020-01-17
    • 2017-03-03
    • 1970-01-01
    • 2021-10-04
    • 2019-11-12
    • 2016-09-19
    • 2016-12-20
    • 2019-12-17
    相关资源
    最近更新 更多