Help with react query and router
Unanswered
Transvaal lion posted this in #help-forum
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?
its for this line, if i delete it react query works
here im using in the component
const usePagination = (
otherParams
: OptionalParams = {}) => {
const searchParams = useSearchParams()
const pathname = usePathname()
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
,
})
useEffect(() => {
const newParams = new URLSearchParams(searchParams.toString())
Object.entries(query).forEach(([key,value]) => {
if(value !== undefined && value !== '') {
newParams.set(key, value.toString())
}
})
router.push(`${pathname}?${newParams.toString()}`)
}, [query, pathname, searchParams])
return
{
query,
setQuery,
}
}its for this line, if i delete it react query works
router.push(`${pathname}?${newParams.toString()}`)here im using in the component
const { query, setQuery } = usePagination()
const { data: milestones, isLoading } = useQuery({
queryKey: ['getMilestonesByProject', '', query],
queryFn: () => getMilestonesProject('', query),
})5 Replies
Sun bear
The router push will refetch the page so i think it will conflict your hook.
You set a state and then refetch the page which means the state is gone.
You set a state and then refetch the page which means the state is gone.
@Sun bear The router push will refetch the page so i think it will conflict your hook.
You set a state and then refetch the page which means the state is gone.
Transvaal lionOP
how i can fix it ? i need to push query params in url
@Transvaal lion how i can fix it ? i need to push query params in url
Sun bear
I would follow this guide:
https://nextjs.org/learn/dashboard-app/adding-search-and-pagination
It is very good
https://nextjs.org/learn/dashboard-app/adding-search-and-pagination
It is very good
@Sun bear I would follow this guide:
https://nextjs.org/learn/dashboard-app/adding-search-and-pagination
It is very good
Transvaal lionOP
ty bro, i will take a look
@Sun bear I would follow this guide:
https://nextjs.org/learn/dashboard-app/adding-search-and-pagination
It is very good
Transvaal lionOP
didnt work, but i set
reactStrictMode: false, in next config and it works o ndevelopment i so consfued