Next.js Discord

Discord Forum

How to prevent router.query.id undefined

Unanswered
ayush_uidev posted this in #help-forum
Open in Discord
Hi everyone i am using nextjs and for the dynamic route im using const router = useRouter() then taking the router.query.id to fetch data on client side but the problem is sometime the router.query.id is undefined and then it gets the id and fetch the data ay it takes time to load how can i prevent that ? it happen when i refresh the page

16 Replies

The first request is undefined and the second one has router.query.id
will this help ? @aardani
its pages router
@ayush_uidev Click to see attachment
Try adding parameter to the queryFn and pass the router.query.nameId from there instead
Just a wild guess
ok ill try
Does it work?
Why im dumb, that only works for useMutation
@ayush_uidev Click to see attachment
Try adding enabled : !!router.query.nameId
That way the queryFn will only be run if the router is not undefined
@aardani Try adding `enabled : !!router.query.nameId`
yes this is working
thanks
@ayush_uidev yes this is working
Nice. Mark the message as solved :sunglasses_1:
ok