TanStackT
TanStack3y ago
2 replies
faint-white

global onError handling

With v4, we had this global handling of an inactive session. Basically when a refresh token expired, we would use this:

const queryClient = useQueryClient();

  useEffect(() => {
    queryClient.setDefaultOptions({
      queries: {
        ...queryClient.getDefaultOptions().queries,
        onError: async (error) => {
          if (isTokenError(error)) {
            queryClient.cancelQueries();

            await modal.open(<ModalInactive />, {
              canClose: false,
              hasBlurredBackground: true,
            });
          }
        },
        retry(failureCount, error) {
          if (isTokenError(error)) {
            return false;
          }

          return (failureCount || 0) < 1;
        },
      },
    });
  }, [i18n, modal, queryClient]);


I could do the same thing in v5 with QueryCache, but that needs to be set in the initating of queryClient, meaning I can't use the queryClient context inside the modal.

What would be the best way to implement this?
Was this page helpful?