Docs
Mutation
Invalidation from Mutation

Invalidation From Mutation

const useEditProductMutation = createMutation(editProduct);
 
function SaveProduct() {
  const { mutate, isWaiting } = useEditProductMutation();
  const { getValues } = useFormContext();
 
  return (
    <button
      disabled={isWaiting}
      onClick={() => {
        const payload = getValues();
        const productId = payload.id;
 
        mutate(payload).then(({ response, error }) => {
          if (response) {
            useProductListInfiniteQuery.invalidate();
            useProductDetailQuery.invalidateSpecificKey({ id: productId });
          }
        });
      }}
    >
      Save
    </button>
  );
}