【问题标题】:Dispatch action on the createAsyncThunk?对 createAsyncThunk 的调度操作?
【发布时间】:2021-08-21 08:57:56
【问题描述】:

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

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

我该怎么做?谢谢!

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

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

【问题讨论】:

    标签: reactjs redux redux-toolkit


    【解决方案1】:

    您可以使用createAsyncThunk 中回调函数的第二个参数来执行此操作:

    export const getProducts = createAsyncThunk("getProducts", async (_, thunkAPI) => {
      thunkAPI.dispatch(actionLoading());
      const response = await axios.get("api");
      return response;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-01
      • 2021-08-27
      • 1970-01-01
      • 2020-12-10
      • 2022-08-17
      • 2021-04-07
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多