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

jam*_*non 3 react-query

因此,我在 onSuccess 中有一些辅助行为,例如分析等。我需要传递到跟踪,不仅是查询/突变的结果(在本例中是突变),还需要传递我传入的参数。似乎只有将其附加到返回“数据”时我才能做到这一点?

    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?
          }
        }
      )
    }
Run Code Online (Sandbox Code Playgroud)

当我不需要它来响应但需要副作用时,这样做似乎很奇怪。有什么想法吗?

    return { ...data, email }
Run Code Online (Sandbox Code Playgroud)

TkD*_*odo 8

对于useMutation,变量作为第二个参数传递给onSuccess这在api 文档中有记录。所以在你的例子中,它很简单:

onSuccess: (data, { product, email }) =>
Run Code Online (Sandbox Code Playgroud)