DEV Community

Discussion on: React Hooks: useState()

Collapse
 
anastawfik profile image
Anas Tawfik

Important note that using an object as state in hooks doesn't behave the same as Class setState,
in Class's setState you can pass only rhe updated object attributes and it will be merged with the current state, meanwhile in Hooks it will replace the whole object so you need to handle the merging yourself, like:
setFruits({...fruits, banana: 'minions'})

Collapse
 
michaeljota profile image
Michael De Abreu

In this case, as same as arrays, it would be better to use the function callback and use the state that the callback gets as a param.