queryCache.find() 未定义

queryCache.find() is undefined

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

这是我的代码:

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

为什么会出现undefined,如何解决?

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

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

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

queryClient.invaliateQueries("posts")

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