好奇为什么我们无法在 onSuccess 中获取查询中的参数?

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

因此,我在 onSuccess 中有一些辅助行为,例如分析等。而且我需要传递给跟踪,不仅是 query/mutation 的结果(在这种情况下是突变),而且还有我传递的 arg。似乎只有将它附加到 return“数据”?

    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?
          }
        }
      )
    }

这样做似乎很奇怪,因为我不需要它来响应,而是为了副作用。有什么想法吗?

    return { ...data, email }

对于useMutation,变量作为第二个参数传递给onSuccess。这记录在 api docs 中。所以在你的例子中,它只是:

onSuccess: (data, { product, email }) =>