【发布时间】:2022-11-22 03:10:14
【问题描述】:
这里我有两个状态切片,我需要在 slice2 中分派 slice1 的方法。
我如何从 callApiSlice 的额外减速器动作中调用切片 1 的减速器动作
const slice1 = createSlice({
initialState,
reducers: {
login: (state) => {
state.login = { email: 'email@gmail.com', api_keys: false};
},
setApiKey: (state) => {
state.login.api_keys = true;
},
},
}
export const callApi = createAsyncThunk(
"call-api",
async (payload, thunkAPI) => {
try {
const res = await axios.post( process.env.REACT_APP_API_URL + "/save", payload);
return res.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.response.data);
}
}
);
const callApiSlice = createSlice({
name: "callApiSlice",
initialState,
reducers: {},
extraReducers: {
[callApi.fulfilled]: (state, action) => {
// how to call Slice 1 reducer's action setApiKey to change in login state
}
}
});
export default callApiSlice.reducer;
【问题讨论】:
-
不,reducer 函数是纯的功能,所以应该绝对没有副作用。
setApiKey需要作为动作发送。这是一个 XY 问题/问题。您在这里真正要解决的用例是什么?你能编辑帖子以包含更完整的minimal reproducible example吗? -
@DrewReese,谢谢,我更新了问题以使问题更清楚。请告诉我。
标签: javascript reactjs redux react-redux redux-toolkit