This is my current data
const [propertyData, setPropertyData] = useState({
propertyName: '',
contacts: [
{
contactName: 'Person1',
contactNumber: 'Number1',
},
{
contactName: 'Person2',
contactNumber: 'Number2',
},
]
})
What I'm trying to achieve is to add another object to contacts
like this
contacts: [
{
contactName: 'Person1',
contactNumber: 'Number1',
},
{
contactName: 'Person2',
contactNumber: 'Number2',
},
{
contactName: 'Person3',
contactNumber: 'Number3',
},
]
What I'm doing is this
setPropertyData({
...propertyData,
contacts: [{ ...propertyData.contacts, contactPerson: 'Person3', contactNumber: 'Number3', }]
})
But what it does is it's updating the current data with just the new one without the old one like this
contacts: [
{
contactName: 'Person3',
contactNumber: 'Number3',
},
]
I'm kind of always confused using the spread operator especially if it's an array or an object I have to to append to. Please help. Thank you!