我正在尝试删除特定数据,该数据应在另一个API中的发布请求后进行更新。值得一提的是,根据该请求,两个API应该更新,其中一个是更新,另一个不更新。
我已经尝试了多种方法来根据成功的POST请求来撤离API,但似乎没有用。
我也尝试添加这样的第二个选项,但没有成功。
{
refetchInactive: true,
refetchActive: true,
}
如此讨论,
也许“键不匹配,因此您试图使缓存中不存在的东西无效。” ,但不确定这是我的情况。
更有趣的是,单击Devtools中的无效按钮,无效本身可以使用。
突变功能:
import { BASE_URL, product1Url, product2Url } from './services/api'
import axios from 'axios'
import { useMutation, useQueryClient } from 'react-query'
export const usePostProduct3 = () => {
const queryClient = useQueryClient()
const mutation = useMutation(
async (data) => {
const url = `${BASE_URL}/product3`
return axios.post(url, data).then((response) => response)
},
{
onSuccess: async (data) => {
return (
await queryClient.invalidateQueries(['prodKey', product1Url]), //this one doesn't work
queryClient.invalidateQueries(['prodKey', product2Url]) //this api refetch/update works
) },
},
)
return mutation
}
我在做什么错?
I am trying to refetch a specific data, which should update after a post request in another API. It's worth mentioning that based on that request two APIs should update and one of them updates, another one not.
I've tried multiple ways to refetch the API based on successful post request, but nothing seems to work.
I tried to also add 2nd option like this, but without success.
{
refetchInactive: true,
refetchActive: true,
}
As it mentioned in this discussion,
maybe "keys are not matching, so you are trying to invalidate something that doesn't exist in the cache.", but not sure that it's my case.
What is more interesting is that clicking the invalidate button in the devtools, the invalidation per se works.
Mutation function:
import { BASE_URL, product1Url, product2Url } from './services/api'
import axios from 'axios'
import { useMutation, useQueryClient } from 'react-query'
export const usePostProduct3 = () => {
const queryClient = useQueryClient()
const mutation = useMutation(
async (data) => {
const url = `${BASE_URL}/product3`
return axios.post(url, data).then((response) => response)
},
{
onSuccess: async (data) => {
return (
await queryClient.invalidateQueries(['prodKey', product1Url]), //this one doesn't work
queryClient.invalidateQueries(['prodKey', product2Url]) //this api refetch/update works
) },
},
)
return mutation
}
What am I doing wrong?
发布评论
评论(2)
几年后,正确的答案是使用
reset
函数。mutation.Reset()
The correct answer, a couple of years later, is to use the
reset
function.mutation.reset()
https://tanstack.com/query/v4/docs/framework/react/guides/mutations
您会这样做
,或者如果您只想使查询无效
You shold do it like this
Or if you just want to invalidate queries