useQuery param is undefined. but query is enabled only if the param is defined.

im trying to set up some searching. and came across this error. i am not sure if i am missing something in my reasoning for why this should work (some react or maybe react-query rules/quirks). SSR is off btw.
9 Replies
benten
benten3y ago
router.query will be undefined on first render, as next.js router blows thus, router.query.term will be undefined router.query?.term would prevent it from throwing an error
benten
benten3y ago
GitHub
notescroll/useClientRouter.tsx at main · B3nten/notescroll
A journaling webapp for tabletop gamers. Contribute to B3nten/notescroll development by creating an account on GitHub.
benten
benten3y ago
(or any next.js router monkeypatch lib)
bigsexy
bigsexyOP3y ago
yea im realizing how trash it is at types. constantly having to cast is a pita. i get that its not defined on first render, but the react-query is not enabled until the router (more precisely router.query.term) is defined. i would have expected the error to say something cannot read property "term" of undefined when trying to determine if the useQuery should be enabled (since router.query is undefined), but instead its throwing an error on .trim()
benten
benten3y ago
Because you can't trim() something that's undefined
bigsexy
bigsexyOP3y ago
ight i guess my point is. shouldnt that particualr useQuery. not be executed untill router.query?.term is defined as is inidicated with the enabled option. IF its the case, then why is it even trying to call .trim() on the param if no react-query should be going out.
benten
benten3y ago
Ah, I see. .trim() is called during the function definition afaik Are you sure router.query.term is a string and not an array?
bigsexy
bigsexyOP3y ago
yea, ive changed the logic to work around that. it ended up being kinda buggy because i was calling a hook useDebounce inside of the useQuery which react understandably didnt like. but originally yea i was a sure it was a string. there was only one function that was modifying it and it looked like this (without the debounce, i add that recently as part of the workaround)
Want results from more Discord servers?
Add your server