Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curious why we can't get at the args in a query, in the onSuccess?

Tags:

react-query

So, I have some ancilliary behaviors in the onSuccess, like analytics and such. And I need to pass in to the tracking, not only the result of the query/mutation (mutation in this case), BUT also an arg I passed in. Seems I can only do it if I attach it to the return "data"?

    export default function useProductToWishList () {
      const queryClient = useQueryClient();
    
      return useMutation(
        async ({ product, email }) => {
          const data = await product.addWishList({ product, email });
          if (data.status === 500 || data.err) throw new Error(data.err);
    
          return data;
        },
    
        {
          onSuccess:(data) => {
            const { product: response = {} } = data?.data ?? {};
    
            queryClient.setQueryData(['products'], {...response });
            analytics(response, email); // HERE. How can I get at email?
          }
        }
      )
    }

seems odd to do, when I don't need it for the response, but for a side effect. Any thoughts?

    return { ...data, email }
like image 216
james emanon Avatar asked Nov 15 '25 13:11

james emanon


1 Answers

for useMutation, the variables are passed as the second argument to onSuccess. This is documented in the api docs. So in your example, it's simply:

onSuccess: (data, { product, email }) =>
like image 138
TkDodo Avatar answered Nov 18 '25 19:11

TkDodo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!