使用 redux-thunk 调用异步操作创建者帮助程序
Invoking an async action creator helper with redux-thunk
我正在尝试为两个异步操作创建者调用辅助方法,但我不确定为什么我不能这样做。分配给 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);
};
因为 signUp
或 signIn
打算只 return 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);
我正在尝试为两个异步操作创建者调用辅助方法,但我不确定为什么我不能这样做。分配给 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);
};
因为 signUp
或 signIn
打算只 return 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);