简体   繁体   English

反应查询:反应性 useMutation 结果

[英]react-query: reactive useMutation results

I'm trying to switch from zustand to react-query as my central cache.我正在尝试从 zustand 切换到 react-query 作为我的中央缓存。 I can get reactive query results with useQuery(queryKey, queryFn) .我可以使用useQuery(queryKey, queryFn)获得反应式查询结果。 However, I want to get reactive results for mutations as well.但是,我也想获得突变的反应结果。 So I tried the following:所以我尝试了以下方法:

useMutation(mutationFn, {
 onSuccess: (d, i) => queryClient.setQueryData(queryKey, d),
});

The problem is how to get the reactive result.问题是如何获得反应结果。

I considered useQuery(queryKey) but the API mentions queryFn is required unless defaults are setup.我考虑useQuery(queryKey)API提到queryFn是必需的,除非设置了默认值。 In my current use case, I don't plan on updating that queryKey unless its via a mutation so I don't have a queryFn .在我当前的用例中,我不打算更新该queryKey除非它通过突变,所以我没有queryFn I can't use queryClient.getQueryData(queryKey) since it's not reactive.我不能使用queryClient.getQueryData(queryKey)因为它不是反应性的。

How can I get the mutation's reactive result?我怎样才能得到突变的反应结果?

You can't use useQuery without a queryFn as it will error out.您不能在没有queryFn useQuery因为它会出错。

https://codesandbox.io/s/react-query-reactive-mutation-results-txcz9?file=/src/index.js https://codesandbox.io/s/react-query-reactive-mutation-results-txcz9?file=/src/index.js

A feature request is in progress to handle the mutations side.正在进行功能请求以处理突变方面。

https://github.com/tannerlinsley/react-query/issues/2304 https://github.com/tannerlinsley/react-query/issues/2304

Any update on this?这事有进一步更新吗? I'm actually having the exact same issue.我实际上有完全相同的问题。 useMutation does not give me any reactive response. useMutation没有给我任何反应性的反应。

I have a POST endpoint that receives certain filters and returns me a set of ids matching those filters.我有一个POST端点,它接收某些过滤器并返回一组与这些过滤器匹配的 ID。 The only way I'm forced to update the data from the query is using it without a queryFn .我被迫从查询中更新data的唯一方法是在没有queryFn的情况下使用它。

So, my question is, how'd you consume the data that came from the POST endpoint?所以,我的问题是,您如何使用来自 POST 端点的数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM