Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React by Example Building the Application Writing a Handler to Confirm Guests

Brad Chandler
Brad Chandler
15,301 Points

Possible improvement?

In the video they use the spread operator to return an object on each iteration of the map function with all of the same properties and the isConfirmed property mutated.

My question : Is the spread operator necessary?

I used the following code

const updatedGuests = this.state.guests.map((guest, index) => {
  if (index === this.indexToChange) {
    guest.isConfirmed = !guest.isConfirmed;
  }
  return guest;
});
this.setState({
  guests: updatedGuests
});

By doing it this way, it shortens the code, alters the target property for the correct guest and still returns the guest each time.

1 Answer

I dont know but it looks like you now have two functions instead of one function with one extra line of code.

Your this.setState is not in a function as far as I can tell so it will run every time your app.js file re renders. Instead of when the updatedGuest function runs.