I'm not sure if it's possible to do, in my React app, I have a <form> with the following input fields:
<TextField />
<TextField />
<TextField />
and I have a state
const [info, setInfo] = useState({
firstName:"",
lastName:"",
colors:[]
})
I know how to set a single field using ES6 Spread like setInfo(...info, firstName:e.target.value), but that doesn't seem to work with an array in Object.
Currently, if I want to set the colors, I go:
<TextField onChange={addColor1}/>
<TextField onChange={addColor2}/>
<TextField onChange={addColor3}/>
and then I create an array colors = [color1, color2, color 3], and lastly I `setInfo({...info, colors:colors})
But I want to know if there's a way to set the color array without having to write repeated functions like addColor1, addColor2 ...
Thank you!
setInfo({...info, colors:[...info.colors, colorToAdd]})