I am getting a syntax error and not sure I understand why:
Current state looks something like this:
people: [{
id: 1,
firstName: 'Eric',
lastName: 'Andrews',
}, {
id: 2,
firstName: 'Rick',
lastName: 'Handsome',
}, {
id: 3,
firstName: 'Yoni',
lastName: 'Andrews',
}],
addNewFriend() {
this.setState({
people: {
[...this.state.people, {
id: this.state.idIncrementor + 1,
firstName: this.state.newPerson['newFirstName'],
lastName: this.state.newPerson['newLastName'],
}]
},
newPerson: ''
})
}
Syntax error: Unexpected token (156:26)
154 | this.setState(
155 | {
> 156 | people: {[...this.state.people,
| ^
157 | {
158 | id: this.state.idIncrementor +1,
159 | firstName: this.state.newPerson['newFirstName'],
I want to merge the this.state.people and a new dictionary. people is current a list of dictionaries and I want to add a new dictionary.
What am I doing wrong?
{[ ]}is just invalid JavaScript. You cannot put an object literal inside an object literal like that. It has nothing to do with spread elements.