'RematchJS - how can I access the state in a model's effect without sending a payload to dispatch
I'm using RematchJS and I'd like to access the state in a model effect without sending a payload to the effect.
The model looks something like this:
export const session = createModel<RootModel>()({
state: {
...
} as SessionModel,
reducers: {
setAuthenticated(state, payload) {
return {
...state,
isAuthenticated: payload,
};
}
},
effects: (dispatch) => ({
async logout(payload, rootState) {
const sessionId = rootState.session.sessionId;
if (sessionId) {
await ApiClient.logout(sessionId);
dispatch.session.setAuthenticated(false);
}
}
}),
});
The problem is that since the payload comes first in an effect, I must send some payload when I dispatch the effect otherwise typescript will complain:
dispatch.session.logout(somePayload);
I work around that by calling dispatch.session.logout(null);
but it feels incorrect.
Is there a nicer solution?
Solution 1:[1]
I think you can just create an effect without any payload param, like this
effects: (dispatch) => ({
async logout(rootState) {
const sessionId = rootState.session.sessionId;
if (sessionId) {
await ApiClient.logout(sessionId);
dispatch.session.setAuthenticated(false);
}
}
}),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kavinda Jayakody |