使用redux-thunk调用异步动作创建者助手

问题描述 投票:0回答:1

我正在尝试为两个异步动作创建者调用辅助方法,但是我不确定为什么不能这样做。分配给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);
};
javascript axios redux-thunk
1个回答
0
投票

由于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);
© www.soinside.com 2019 - 2024. All rights reserved.