queryCache.find() is undefined



我有一个问题与反应查询。当我从列表中删除带有突变的项时,我需要刷新页面以查看更改。当我尝试queryCache.find('key')我得到未定义。我也没有一个方法queryCache.refetchQueries()(看到在教程,但它似乎是深度。)

下面是我的代码:
const { data, status, error, refetch } = useQuery('posts', fetchPosts);
const queryCache = new QueryCache();
const query = queryCache.find('posts');
console.log(query); // undefined

这是一个突变:

const mutation = useMutation(deletePost, {
onSuccess:  () => {
// can I here somehow do a refetch?
}
});
const deleteOne = async (id: string) => {
mutation.mutate(id)
}

为什么我得到定义和如何解决这个问题?

您使用的是哪个版本的react-query ?在v3(最新的主要版本)中,您需要使用queryClient而不是queryCache:

const queryClient = useQueryClient()
const posts = queryClient.getQueryData('posts')

对于突变后的重取,通常最好使用:

queryClient.invaliateQueries("posts")

这也是在文档中显示的方式:invalidations-from-mutations

最新更新