0
votes

I have two action in my redux actions... now I have a login form that makes a post request.. now I want to call another action from within the login action. Now i connected the login function but not the setLoading function

e.g

export const loginUser = ()=>dispatch=>{
 setLoading();
}

export const setLoading = ()=>({
type: SET_LOADING
})
1
redux-thunk is what you are looking for - Sergiu Paraschiv
You can dispatch an action after that you resolve it in the used milddleware in your project. an action is a plain object, the code you provided are action creators - Incepter

1 Answers

1
votes

You can use the logic of this answer to help you. An example is given in this answer(That's an example of getting products).

React Redux fetching data from backend approach

For your problem, it's best to pay attention to this part of the link I gave you above, since you can use that logic for your program.

Notice this in the link above

// redux/product/product.actions.js
import { ShopActionTypes } from "./product.types";
import axios from "axios";

export const fetchProductsStart = () => ({
  type: ShopActionTypes.FETCH_PRODUCTS_START
});

export const fetchProductsSuccess = products => ({
  type: ShopActionTypes.FETCH_PRODUCTS_SUCCESS,
  payload: products
});

export const fetchProductsFailure = error => ({
  type: ShopActionTypes.FETCH_PRODUCTS_FAILURE,
  payload: error
});

export const fetchProductsStartAsync = () => {
  return dispatch => {
    dispatch(fetchProductsStart());
    axios
      .get(url)
      .then(response => dispatch(fetchProductsSuccess(response.data.data)))
      .catch(error => dispatch(fetchProductsFailure(error)));
  };
};