Would like to know if there is a way to update nested object states in React using useState()
import React, {useState} from 'react'
const MyComp = () => {
const [colors, setColors] = useState({colorA: 'RED', colorB: 'PURPLE'});
return (
<div>
<span>{colors.colorB}</span>
<button onClick={() => setColors({...colors, colors: { colorB: 'WHITE'}})}>CLICK ME</button>
</div>
)
}
export default MyComp;
I was thinking to use useReducer() but I read that It's normally use for more complex states and maybe there is a solution for this case just using useState()
Any ideas?
Thx in advance