我有一个 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;
});
您可以使用回调函数的第二个参数来做到这一点createAsyncThunk
:
export const getProducts = createAsyncThunk("getProducts", async (_, thunkAPI) => {
thunkAPI.dispatch(actionLoading());
const response = await axios.get("api");
return response;
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)