我可以访问extraReducers内的dispatch吗?如果失败或完成,我想基于 asyncThunk 函数调度操作。
const todosSlice = createSlice({
name: "todos",
initialState,
reducers: {},
extraReducers: (builder) => {
console.log(builder);
builder.addCase(fetchTodos.pending, (state) => {
return { ...state, isFetching: true };
});
builder.addCase(fetchTodos.fulfilled, (state, action) => {
return { ...state, isFetching: false, data: action.payload.data };
});
builder.addCase(fetchTodos.rejected, (state, action) => {
console.log("Can console here if reject occurs");
return { ...state, isFetching: false, error: action.error };
});
},
});
export const todosSliceState = (state) => state.todos;
export default todosSlice.reducer;
所以我想在 fetchTodos.fulfilled 中dispath操作,并且该操作只会改变其他 reduxSlice 的状态
我可以访问
内的dispatch
吗?我要派送 基于 asyncThunk 函数的操作(如果失败或完成)。extraReducers
不,因为减速器是纯函数,所以它们不会有像调度其他操作这样的副作用。只需将
fetchTodos.fulfilled
添加为具有您想要更新状态的另一个切片/减速器中的减速器案例即可。