Answer the question
In order to leave comments, you need to log in
Why is a separate state property not set?
*code slightly simplified for readability*
[user,setUser]=React.useState({
name: 'noName',
pull: [{},{},{}],
})
<Hero addToCamp={() => addToCamp()/>)
const addToCamp = () => {
let newHeroes= ...
setUser({pull: newHeroes}) // Что тут не так?..
}
}
Answer the question
In order to leave comments, you need to log in
In class components, this.setState superficially submerged the passed value into the state. In a hook, it simply replaces, and you need to merge manually, for example, through spreads. That's the way the world works. There is only one state in the class, and there can be several useState. For example here one could do useState for name and useState for pull
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question