'How add new object property while updating state in React
Basically I want to add a new property id to the state object which is formData I tried the way below mention but its not working
structure of formData object
{
Fname : Tony
Lname : Stark
}
I want to Add Id property to it
{
id : 01
Fname : Tony
Lname : Stark
}
This is what I tried :
let arr = {...formData}
arr.id = 1
setFormData({
arr
})
Solution 1:[1]
do it like this
setFormData({...formData, id:1})
Solution 2:[2]
setFormData({...formData, id: '01'})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lawrence arkoh |
| Solution 2 | Myrto Pirli |
