Z
Z
zzeeezzz2021-08-29 23:14:12
React
zzeeezzz, 2021-08-29 23:14:12

How to pass asynchronous parameters to action (redux, thunk)?

I have a problem when passing data to action. I have an actions.js file that has a setCart(value) function that accepts a value as input.

export const setCart = (value) => ({type: ADD_TO_CART, payload: value});

There is also an app.js file in which I pass the value to the action
const App = ({setCart}) => {

  const fetchCart = async () => {
    const res = await commerce.cart.retrieve()
    setCart(res);
  };

}

const mapDispatchToProps = {
  setCart
}

export default connect(null, mapDispatchToProps)(App);


But since the passed parameter is asynchronous, I can't properly pass it to actions.js. I know that I need to use middleware(thunk), but I don't understand how to use it when passing an asynchronous parameter.
Can you help figure it out?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question