Timeline for answer to Whats the best way to update an object in an array in ReactJS? by undefined
Current License: CC BY-SA 4.0
Post Revisions
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 9, 2022 at 9:40 | comment | added | ViktorMS | This answer can be improved by explaining what is going on in the code. | |
| Dec 8, 2021 at 4:05 | comment | added | Nick Parsons |
@user3884753 {text} is shorthand for {text: text}. As text is defined as a variable, you can use it as both the name and value in the object without needing to define it twice. This came with ES6, and is sometimes referred to as "shorthand property names"
|
|
| Mar 16, 2021 at 15:53 | comment | added | user3884753 | @Notloved Why is it just ({}, el, { text }) or {...el, text} in the two ways you mentioned and not ({}, el, { text: text }) or {...el, text: text}. How does it know that it has to update value of 'text' key and not the other ones? Or if the 'text' word in your answer refers to the key, where are we passing the value 'text'? I guess I am missing some fundamental understanding of how it works | |
| Oct 30, 2019 at 10:12 | comment | added | nevermind777 |
Great answer, thanks! Since you shouldn't access state in setState, this is a safer version of it: this.setState(prevState => ({ data: prevState.data.map(el => (el.id === id ? { ...el, text } : el)) }))
|
|
| Feb 12, 2019 at 7:09 | history | edited | undefined | CC BY-SA 4.0 |
added 256 characters in body
|
| Aug 3, 2018 at 1:24 | history | edited | undefined | CC BY-SA 4.0 |
deleted 5 characters in body
|
| Oct 2, 2017 at 2:07 | history | answered | undefined | CC BY-SA 3.0 |