对 createAsyncThunk 的调度操作?

Dispatch action on the createAsyncThunk?

我有一个 thunk 动作是由 createAsyncThunk 创建的。我想在调用 api 更新状态之前发送一个动作。

我不想使用操作 getProducts.pending,因为我想为其他 thunk 操作调度 actionLoading()

我该怎么做?谢谢!

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

  // I want dispatch action actionCallAPIPending like that
  // dispatch(actionLoading());
  const response = await axios.get("api");
  return response;
});

你可以在 createAsyncThunk:

中使用回调函数的第二个参数来做到这一点
export const getProducts = createAsyncThunk("getProducts", async (_, thunkAPI) => {
  thunkAPI.dispatch(actionLoading());
  const response = await axios.get("api");
  return response;
});