-
|
As far as I've understood, the only way to access global events (like const queryClient = new QueryClient({
queryCache: new QueryCache({ onError }),
mutationCache: new MutationCache({ onError }),
});This means there's no way for child components to hook into these events, which could be useful. For example, my use-case is that I would like to trigger the showing of a "You've been logged out"-modal when anything fails with a Preferably, I would just like to be able to add a subscription to Something like this, maybe? const client = useQueryClient()
useEffect(() => {
const unsubscribe = client.subscribe('error', someErrorHandler);
return unsubscribe;
}, [client])Maybe even allow subscribing to other statuses like 'success' and 'loading' as well? It seems I can set |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
the global cache callbacks are perfect for this, e.g.: if you want this only for some components, you can check the query key / mutation key inside that handler, as it gets that passed as well, or check the |
Beta Was this translation helpful? Give feedback.
-
|
So, it's a bit awkward, but I guess the only way to subscribe to global events, after the client and caches have already been created, is to subscribe individually to the An example looking for a certain kind of error, function useIsUnauthorized(): boolean {
const [isUnauthorized, toggle] = useReducer(() => true, false);
const client = useQueryClient();
// Query Cache subscription
useEffect(
() =>
client.getQueryCache().subscribe((event) => {
if (
event?.type === 'queryUpdated' &&
event.action.type === 'error' &&
event.action.error instanceof UnauthorizedError
) {
toggle();
}
}),
[client]
);
// Mutation Cache subscription
useEffect(
() =>
client.getMutationCache().subscribe((mutation) => {
if (mutation?.state.error instanceof UnauthorizedError) {
toggle();
}
}),
[client]
);
return isUnauthorized;
}Could probably be wrapped up in a more generic |
Beta Was this translation helpful? Give feedback.
So, it's a bit awkward, but I guess the only way to subscribe to global events, after the client and caches have already been created, is to subscribe individually to the
QueryCacheand theMutationCache, and then sort out the type of event you're after manually.An example looking for a certain kind of error,
UnauthorizedError: