TanStackT
TanStack3y ago
8 replies
dangerous-fuchsia

Mutation -> Invalidation + Refetch or setQueryData

Currently in our app, we have a lot of scenarios where we do the following:

  const mutation = useMutation(
    (params) => {
      return fetchFunction({
        params
      });
    },
    {
      retry: 1,
      onSuccess: async () => {
        await queryClient.invalidateQueries(
          [QUERY_KEYS],
          {
            refetchType: 'all',
          }
        );
        refetch();
      },


Having read around I am feeling that we could instead use setQueryData here in the onSuccess of the useMutation?

What are the pros / cons of this, is there a recommended best practice where we update some data via a mutation and need our results immediately. (The queries would be 'active' but don't seem to fetch data until refocus / navigation after invalidation without the refetch call).

Thanks!
Was this page helpful?