I am adding this method to a React Component:
removeContact = (contact) => {
this.setState((currentState) => ({
contacts: currentState.contacts.filter((c) => {
return c.id !== contact.id
})
}))
The method modify the state of the Component each time an user press an element's delete button, then re-render the page showing only the not deleted elements. The method is actually working fine, but I don't understand why do I need the second curly braces containing the brackets in the arrow function in the second line.
Let me mark what curly braces I am talking about
=> ({
//contacts: currentState...
}))
I know an arrow funtion can use either with braces or brackets depending on the content. But what is the function of curly braces containing brackets?
Thank you very much for your time
Using "(" makes the function auto return. Its the same as doing
=> {
return {} //contacts: currentState...
})
The {
inside is just a regular object bracket.
So your () => ({})
is function that automatically returns an object