Handling side-effects of rtk queries #3101
-
Hi there! How to best handle side-effects of rtk queries? E.g. I have a query that checks whether a user is still logged in. When they get logged out, I would like to trigger a popup. function useAuth () {
const {authData} = useAuthQuery();
useEffect(() => {
}, [authData]);
} but that's not what listenerMiddleware.startListening({
predicate: (action) => action.type === 'auth/executeQuery/fulfilled' && action.meta.arg.endpointName === 'authStatus' && !action.payload.loggedIn,
effect: (_action, listenerApi) => {
listenerApi.dispatch(showLoggedOutPopUp());
}
}); But I can't actually get the action whose type I'm referencing out of the api, so this looks like it is relying on implementation details. What's the best approach? If the second one is of interest, would it make sense to expose these actions to be able to react to them? There is already ".internalActions", but this doesn't expose the query action in a way that can be reached, because the action itself is created via a thunk which isn't exposed. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You can use the endpoint matcher: listenerMiddleware.startListening({
matcher: api.endpoints.myEndpoint.matchFulfilled,
effect: (action, listenerApi) => {}
}) Or if you do have an additional check: predicate: (action) => api.endpoints.myEndpoint.matchFulfilled(action) && !action.payload.loggedIn |
Beta Was this translation helpful? Give feedback.
You can use the endpoint matcher:
Or if you do have an additional check: