React useState returns undefined
Unanswered
Red-legged Kittiwake posted this in #help-forum
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
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 workPolar 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
could this be the issue?https://stackoverflow.com/questions/68861996/usestate-undefined-in-react-just-after-set
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?
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