重新获取或失效后查询永远不会停止获取

发布于 2025-01-15 23:07:50 字数 1239 浏览 4 评论 0原文

第一次获取有效,重置查询也是如此。数据随即是新鲜的,然后在 5 秒后变得陈旧。

但是,我想在应用突变后重新获取或使查询无效,但每当我这样做时,数据都会不断重新获取并且永远不会返回: DevTools scr 显示刚刚重新获取的数据

与我直接使用 DevTools 时相同。

我的 useQuery 挂钩:

export const useFetchCandidates = (id: string) => {
    
    return useQuery<Entry<IKandidatFields>[], Error>(
        'candidates',
        async () => {
            const res = await getCandidates(id)
            return res
        },
        {
            staleTime: 5000,
            cacheTime: 10,
        }
    )
}

使用挂钩访问数据:

const {
        data: candidates,
    }: UseQueryResult<Entry<IKandidatFields>[], Error> = useFetchCandidates(id!)

突变:

const mutation = useMutation(
        () =>
            sendCandidateToNextStage(candidateID, utlysning).then(() =>
                getCandidates(id!)
            ),
        {
            onMutate: () => {
                queryClient.cancelQueries(['candidates', id])
            },
            onSettled: () => {
                queryClient.resetQueries(['candidates', id])
            },
        }
    )

Fetching for the first time works, same goes for resetting the query. The data is fresh right after, then becomes stale after 5 seconds.

However, I want to refetch or invalidate the queries after applying mutations, but whenever I do so, the data just keeps refetching and never returning:
DevTools scr showing data just refetching

Same when i use the DevTools directly.

My useQuery hook:

export const useFetchCandidates = (id: string) => {
    
    return useQuery<Entry<IKandidatFields>[], Error>(
        'candidates',
        async () => {
            const res = await getCandidates(id)
            return res
        },
        {
            staleTime: 5000,
            cacheTime: 10,
        }
    )
}

Using the hook to access data:

const {
        data: candidates,
    }: UseQueryResult<Entry<IKandidatFields>[], Error> = useFetchCandidates(id!)

The mutation:

const mutation = useMutation(
        () =>
            sendCandidateToNextStage(candidateID, utlysning).then(() =>
                getCandidates(id!)
            ),
        {
            onMutate: () => {
                queryClient.cancelQueries(['candidates', id])
            },
            onSettled: () => {
                queryClient.resetQueries(['candidates', id])
            },
        }
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2025-01-22 23:07:50

这是通过使用 get axios 请求而不是直接利用 cms 客户端解决的...仍然不知道为什么当它们都返回相同的对象时,一个而不是另一个起作用。

当使用内容丰富的 API 客户端时:

export async function getCandidates(
    id: string
): Promise<Entry<IKandidatFields>[]> {
    const res = await client
        .getEntries<IKandidatFields>({
            content_type: 'kandidat',
        })

    return res
}

它不断地获取并且从未工作。

然而,当使用 axios 请求作为

export const getCandidatesFromAPI = async (id: string) => {
    const { data } = await axios.get(
        `https://cdn.contentful.com/spaces/${spaceID}/environments/${environmentId}/entries?access_token=${accessToken}&content_type=${contentTypeKandidat}`
    )
    return data
}

突变函数时,一切都运行良好。

This was solved by using a get axios request instead of utilizing the cms client directly... still don't know why the one works instead of the other when both of them return the same object.

When using the contentful API client:

export async function getCandidates(
    id: string
): Promise<Entry<IKandidatFields>[]> {
    const res = await client
        .getEntries<IKandidatFields>({
            content_type: 'kandidat',
        })

    return res
}

it was constantly fetching and never worked.

However, when using an axios request instead

export const getCandidatesFromAPI = async (id: string) => {
    const { data } = await axios.get(
        `https://cdn.contentful.com/spaces/${spaceID}/environments/${environmentId}/entries?access_token=${accessToken}&content_type=${contentTypeKandidat}`
    )
    return data
}

as the mutation function, everything worked perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文