【发布时间】:2020-07-18 00:54:58
【问题描述】:
我正在尝试为两个异步操作创建者调用辅助方法,但我不确定为什么我不能这样做。分配给signUpOrSignIn 的函数应该被signUp 和signIn 调用。这些都在同一个文件中。我认为这个问题涉及我的函数声明或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);
};
【问题讨论】:
-
你在哪里调用
signIn或signup? -
在 React 组件内的处理程序中。我知道
signIn和signUp正在被调用,但都没有调用signUpOrSignIn。 -
你可以尝试在两个函数声明中都使用
return signUpOrSignIn("/users", email, password, history);。您在箭头函数中使用{}语法,但没有返回任何内容。 -
刚刚尝试过,没有运气。
-
您可以尝试从
signUp和signIn中删除async dispatch。他们都只是返回一个函数。无论如何,您都没有在其中使用dispatch。我认为这就是为什么signUporSignIn没有被调用。如果您不想删除它,请尝试像这样调用它们signUp(params)()signIn(params)()
标签: javascript axios redux-thunk