Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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;
  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

Sean Paulson
Sean Paulson
6,667 Points

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.