目前,我正在使用 Typescript 深入研究 Redux Toolkit,但我遇到了注销操作。它基本上不应该有任何有效负载,但应该修改 localStorage 和 axios 配置。我知道两种工作方式略有不同。
prepare一种是在 a 内部定义带有回调的减速器createSlice:
const authSlice = createSlice({
name: "authentication",
initialState,
reducers: {
logout: {
reducer: () => initialState,
prepare: () => {
api.logout();
return { payload: undefined, error: undefined, meta: undefined };
},
},
},...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,准备回调的类型签名迫使我显式返回payload,error和meta属性。
createAction另一种方法是将 a与回调一起使用prepare:
export const logout = createAction("authentication/logout", () => {
api.logout();
return { payload: undefined };
});
Run Code Online (Sandbox Code Playgroud)
这是一个看起来更好一点的解决方案。至少我不必通过errorand meta。payload …