gpt4 book ai didi

reactjs - 如何将过滤器变量传递给带有 pageParam 的 useInfiniteQuery?

转载 作者:行者123 更新时间:2023-12-02 18:09:30 33 4
gpt4 key购买 nike

我试图在 useInfiniteQuery 中将 queryKey 变量与 pageParam 一起传递?我已经尝试了一段时间但是:

  • 无法获取页面数据
  • 在某些情况下,数据会重复呈现。

我应该如何传递变量?

export const fetchInfiniteVariants = async (
filters = {},
{ pageParam = 0 }
) => {
const records = await axios.get(baseURL, {
headers: authHeader,
params: {
pageSize: 24,
offset: pageParam,
fields: [
"name",
"packshot",
"packshot_size",
"brand_logo",
"price",
"slug",
],
// filterByFormula: `({validated} = 1)`,
filterByFormula: `(${filterByFields(filters)})`,
"sort[0][field]": "priority",
"sort[0][direction]": "asc",
},
})
return records
}

export const useInfiniteVariantsQuery = (
initialRecords,
offset,
filters = { brand: "HAY" }
) => {
const infiniteVariantsQuery = useInfiniteQuery(
["infiniteVariants", filters],
() => fetchInfiniteVariants(filters),
{
initialStale: true,
staleTime: 6000,
getNextPageParam: (lastPage, pages) => lastPage.data.offset,
}
)
return {
...infiniteVariantsQuery,
}
}

最佳答案

您传递给 useInfiniteQuery 的 queryFn 将请求上下文作为该回调的参数,如 useInfiniteQuery page 中所述:

Receives a QueryFunctionContext object with the following variables:

  • queryKey: EnsuredQueryKey: the queryKey, guaranteed to be an Array
  • pageParam: unknown | undefined

您可以从中解构并检索您的 queryKey,如下所示:


export const useInfiniteVariantsQuery = (
initialRecords,
offset,
filters = { brand: "HAY" }
) => {
const infiniteVariantsQuery = useInfiniteQuery(
["infiniteVariants", filters],
({ queryKey, pageParam }) => fetchInfiniteVariants(queryKey[1], pageParam),
{
initialStale: true,
staleTime: 6000,
getNextPageParam: (lastPage, pages) => lastPage.data.offset,
}
)
return {
...infiniteVariantsQuery,
}
}

关于reactjs - 如何将过滤器变量传递给带有 pageParam 的 useInfiniteQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72624944/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com