Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispatch action on the createAsyncThunk?

I hava a thunk action was created by createAsyncThunk. I want to dispatch an action before call api to update state.

I don't want use action getProducts.pending because I want dispatch actionLoading() for other thunk actions.

How I can i do it? Thanks!

export const getProducts = createAsyncThunk("getProducts", async () => {

  // I want dispatch action actionCallAPIPending like that
  // dispatch(actionLoading());
  const response = await axios.get("api");
  return response;
});
like image 480
queeng Avatar asked Nov 24 '25 13:11

queeng


1 Answers

You can do it like this with the second param of callback function in the createAsyncThunk:

export const getProducts = createAsyncThunk("getProducts", async (_, thunkAPI) => {
  thunkAPI.dispatch(actionLoading());
  const response = await axios.get("api");
  return response;
});
like image 113
Viet Avatar answered Nov 27 '25 04:11

Viet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!