Next.js Discord

Discord Forum

React useState returns undefined

Unanswered
Red-legged Kittiwake posted this in #help-forum
Open in Discord
Red-legged KittiwakeOP
I have a component where I declare a state and pass it to a child
The child component accepts it and tries to display and update it, but when it tries, the state is undefined

22 Replies

Red-legged KittiwakeOP
is it the order of rendering?
just a thought
Polar bear
setSelected(currentValue)
like this use it
Red-legged KittiwakeOP
i tried
setSelected([...selected, currentValue]);
but it would say undefined is not iterable
if i set a default value to selected in TagsMenu when receiving the props, setSelected also doesn't work
Polar bear
the tagMenu is child component right? and it is exported exactly?
Red-legged KittiwakeOP
yea
Polar bear
weird
it should work and your screenshot is good
Red-legged KittiwakeOP
Polar bear
share codes in here
Red-legged KittiwakeOP
but even when i initalize the useState with values like this it's still undef
@Polar bear share codes in here
Red-legged KittiwakeOP
the two files?
Polar bear
console.log(selected)
at parent compeont what is the log?
Red-legged KittiwakeOP
@Polar bear i realized i was on an old version of the page (at a different route), and this one didn't pass the props selected and setSeleted. this page works. thanks for your help!
Polar bear
ok