Next.js Discord

Discord Forum

Problem with react query and searchParams

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
Hi, I am using react query with a custom hook for pagination but the query is not executed, it is always pending for some reason and it does not make the call, is it because of the line where I push the queries, did it happen to anyone?

import { OptionalParams, Pagination } from '@/types/commonTypes'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { useEffect, useState } from 'react'

const usePagination = (otherParams: OptionalParams = {}) => {
  const searchParams = useSearchParams()
  const router = useRouter()

  const defaultParams: Pagination = {
    page: Number(searchParams?.get('page')) || 1,
    limit: Number(searchParams?.get('limit')) || 10,
    search: searchParams?.get('search') || '',
  }

  const [query, setQuery] = useState<Pagination & OptionalParams>({
    ...defaultParams,
    ...otherParams,
  })

  const [search, setSearch] = useState('')

  useEffect(() => {
    const newParams = new URLSearchParams(searchParams)
    Object.entries(query).forEach(([key, value]) => {
      if (value !== undefined && value !== '') {
        newParams.set(key, value !== null ? value.toString() : '')
      }
    })
    router.push(`?${newParams.toString()}`)
  }, [query, currentPathname, router, searchParams])

  return {
    query,
    search,
    setSearch,
    setQuery,
  }
}

export default usePagination

if for this line, if i delete it react query works but dosent push to the url
router.push(`?${newParams.toString()}`)


const { query, setQuery } = usePagination()

  const { data: milestones, isLoading } = useQuery({
    queryKey: ['getMilestonesByProject', '', query],
    queryFn: () => getMilestonesProject('', query),
  })


It always stays in pending but never actually makes the request.

12 Replies

@Transvaal lion I recommend you to use this library
https://nuqs.47ng.com/
Transvaal lionOP
ty, i'll take a look. But do you know why im getting this error? i would like to do this without libraries @James4u
first of all, one question, you must be doing client-side data fetching, what's the reason to have query params in the url?
@James4u first of all, one question, you must be doing client-side data fetching, what's the reason to have query params in the url?
Transvaal lionOP
yeah im doing cs fetching, i added query params to can share the url and keep the page when im get back
I am not pretty sure but I guess it's because React Query relies on detecting changes in query keys or other dependencies.
but router.push() triggers client side navigation which doesn't cause a full page reload
  useEffect(() => {
    if (Object.keys(query).length) {
      refetch();
    }
  }, [query, refetch]);

I think you should have this kind of logic maybe?
Transvaal lionOP
it dosent works, im so confused
one sec @Transvaal lion are you on app router?
Transvaal lionOP
yeap
"@tanstack/react-query": "^5.51.9",
"next": "14.2.3",
then I recommend server side data fetching
with the library I shared