简体   繁体   中英

Invoking an async action creator helper with redux-thunk

I'm trying to invoke a helper method for two async action creators, but I'm unsure why I can't do so. The function assigned to signUpOrSignIn should get invoked by both signUp and signIn . These are in the same file. I'm thinking the issue involves either my function declaration or handling of async dispatch . Would appreciate your feedback. Thanks!

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);
};

Since signUp or signIn intend to just return the signUpOrSignIn .

You can remove async dispatch .

Like so:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM