T
TanStack3y ago
ratty-blush

Can i get feedback on this pagination code?

Notes: The code is working well, to my understanding, but i am not sure if thats how its suppose to be. If anyone can give me feedback if its written optimally, or it has flaws, please let me know.

I also attached a picture of the query dev tools

const fetcher = async (url, params) => {
const res = await axios.get(url, {
params
})
return res.data
}

export default function Inspiration() {
const router = useRouter()
const page_size = 12
const queryClient = useQueryClient()

const { data, isLoading, isError, isPreviousData } = useQuery(
{
queryKey: ['projects', router.query.page, page_size],
queryFn: () =>
fetcher('/api/projects', {
page_num: router.query.page,
page_size
})
},
{
keepPreviousData: true
}
)

const tags = useQuery(['tags'], () => fetcher('/api/project-tags'), {
staleTime: Infinity,
cacheTime: Infinity
})

useEffect(() => {
queryClient.prefetchQuery(
['projects', Number(router.query.page) + 1, page_size],
() =>
fetcher('/api/projects', {
page_num: Number(router.query.page) + 1,
page_size
})
)
}, [router.query.page, queryClient, page_size])

if (isLoading) return <Loading />
if (isError) return <div>error</div>
Notes: The code is working well, to my understanding, but i am not sure if thats how its suppose to be. If anyone can give me feedback if its written optimally, or it has flaws, please let me know.

I also attached a picture of the query dev tools

const fetcher = async (url, params) => {
const res = await axios.get(url, {
params
})
return res.data
}

export default function Inspiration() {
const router = useRouter()
const page_size = 12
const queryClient = useQueryClient()

const { data, isLoading, isError, isPreviousData } = useQuery(
{
queryKey: ['projects', router.query.page, page_size],
queryFn: () =>
fetcher('/api/projects', {
page_num: router.query.page,
page_size
})
},
{
keepPreviousData: true
}
)

const tags = useQuery(['tags'], () => fetcher('/api/project-tags'), {
staleTime: Infinity,
cacheTime: Infinity
})

useEffect(() => {
queryClient.prefetchQuery(
['projects', Number(router.query.page) + 1, page_size],
() =>
fetcher('/api/projects', {
page_num: Number(router.query.page) + 1,
page_size
})
)
}, [router.query.page, queryClient, page_size])

if (isLoading) return <Loading />
if (isError) return <div>error</div>
No description
6 Replies
flat-fuchsia
flat-fuchsia3y ago
I might be missing something but I think the useEffect() is not required: when router.query.page or page_size changes, useQuery will automatically refetch the data since those values are in the queryKey. Was there another reason for using prefetchQuery?
ratty-blush
ratty-blushOP3y ago
hello @julien . I followed the example from react query's website for paginated queries. On examples, this code was written by them to prefetch the next page.
vicious-gold
vicious-gold3y ago
If you prefetch the next page right away, you need to make sure that the staleTime is sufficient? Otherwise when the query for next page gets active, it might be already stale
flat-fuchsia
flat-fuchsia3y ago
Oh my bad, I didn't realize it was the next page that was being prefetched!
ratty-blush
ratty-blushOP3y ago
ok! how do i do that? I assume that if i don't include the staleTime method, it assumes it wiill always be the latest
flat-fuchsia
flat-fuchsia3y ago
Even if the data is stale when the query for the next page gets active, data will be served from the cache and refetch will happen in the background so that should be fine as far as I know. The code looks good to me overall!

Did you find this page helpful?