Skip to content

Ability to subscribe to global events (like onError) in child components #3533

Answered by Svish
Svish asked this question in Ideas
Discussion options

You must be logged in to vote

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 QueryCache and the MutationCache, and then sort out the type of event you're after manually.

An example looking for a certain kind of error, UnauthorizedError:

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.ac…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@Svish
Comment options

@TkDodo
Comment options

@Svish
Comment options

@Svish
Comment options

Comment options

You must be logged in to vote
1 reply
@7amou3
Comment options

Answer selected by Svish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
3 participants