why id is undeifined??
Answered
Polar bear posted this in #help-forum
73 Replies
the hook you need is [
useSearchParams
](https://nextjs.org/docs/app/api-reference/functions/use-search-params), that useRouter
from next/router
doesn't work in the app routerPolar bearOP
null
send full code
@Polar bear here
Polar bearOP
ok
wait a minuet
its ok, take your time
I mean
maybe just the
useEffect()
try
useEffect(()=>{
console.log('server id': id)
},[])
?your useEffect does not have dependency array?
Polar bearOP
:o
try this and see if the param is printed out
add
id
in useEffect dependencyAnswer
and check before using it
Polar bearOP
how to add it?
in line 68, that array
Polar bearOP
like this?
not in quotes
Polar bearOP
ok
like this??
like it should print even with
[]
Polar bearOP
hmm
and i didnt see console.log() outside of the conditionals
and codesandbox is giving me a hard time with next.js so i can't give you a demo right now
Polar bearOP
hmm
give me pls
give you what
Polar bearOP
pull requests
just try this
Polar bearOP
ok
null
okay try putting [id] then
useEffect(()=>{
console.log('server id': id)
},[id])
Polar bearOP
ok
hmm
Polar bearOP
why print two?
coz first is component on mount
then second is when
then second is when
id
value changedits called a dependency array so the
useEffect
is run when the value inside that array changedPolar bearOP
oh
and ofc, also, at the first time the component is mounted
does it solves you problem?
Polar bearOP
no...
why don't work function?
but the id is no longer undefined right?
Polar bearOP
yes
so the question "why id is undefined" is answered right?
Polar bearOP
yep
why id is null
wt...
console.log(idd, cnt == 0 & idd)
yes you have to check if first just like he said
if (idd) ....
Polar bearOP
why logged integer??
@Alfonsus Ardani
hmm im not sure why
maybe try to troubleshoot it
Polar bearOP
hmm