Redux 工具包 AsyncThunk 调度

问题描述 投票:0回答:1

我可以访问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 的状态

redux frontend redux-toolkit redux-thunk
1个回答
0
投票

我可以访问

dispatch
内的
extraReducers
吗?我要派送 基于 asyncThunk 函数的操作(如果失败或完成)。

不,因为减速器是纯函数,所以它们不会有像调度其他操作这样的副作用。只需将

fetchTodos.fulfilled
添加为具有您想要更新状态的另一个切片/减速器中的减速器案例即可。

© www.soinside.com 2019 - 2024. All rights reserved.