What does the line 22 & 25 do?

Screen Shot 2020-11-22 at 7.32.07 PM

Lines 22 and 25 are using the spread operator to maintain the existing values of the state object. Here’s a write-up about the usage of the spread operator on object literals: Spread syntax (…)

One thing to note is that line 22 is actually not necessary because of how React’s setState() works. The other key/value pairs of the state object would still be maintained because it merges.

However, line 25 is necessary if there are any other key/value pairs in the newTask object not being updated. If it wasn’t there, then any other key/value pairs not updated would be lost because the entire newTask object is being replaced by setState()

This thread contains more details, including some sample code I put together to demonstrate the merging process if you’d like to review it:

1 Like