Currently I'm digging into Redux Toolkit with Typescript and I'm stuck with a logout action. It basically shoudn't have any payload at all, but should modify localStorage and axios config. I'm aware of two ways that work slightly different.
One is to define reducer with a prepare callback inside of a createSlice:
const authSlice = createSlice({
name: "authentication",
initialState,
reducers: {
logout: {
reducer: () => initialState,
prepare: () => {
api.logout();
return { payload: undefined, error: undefined, meta: undefined };
},
},
},...
In this case type signature of the prepare callback forces me to explicitly return a payload, error and meta properties.
Another way is to use a createAction with a prepare callback:
export const logout = createAction("authentication/logout", () => {
api.logout();
return { payload: undefined };
});
This is a bit better looking solution. At least I don't have to pass an error and meta. But still forced to return payload even though its not needed.
A bit confused why prepare function signatures differs, but thats not the point.
My question is how you would handle such cases. Maybe I'm doing something completelly wrong?
I understand that there is also option to use middleware, but setting up middleware that handles only one action looks like an overhead.