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 Connecting the Confirm Guests Handler

benjaminmosery
benjaminmosery
6,346 Points

The names in my Application disappear when I click the 'Confirmed' Box?

Whenever I run my Application and click the 'Confirmed' checkbox, the names in my Application disappear. Here is what I am working with:

import React, { Component } from 'react';
import './App.css';
import Guestlist from './Guestlist';


class App extends Component {
  state = { 
    guests: [
     {
      name: 'Julian',
      isConfirmed: false

    }, 
    {
      name: 'Alba',
      isConfirmed: false 

    },
    {
      name: 'Ben',
      isConfirmed: true  

    }

    ]
  };


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

GetTotalInvited = () => this.state.guests.length;
//GetAttendingGuests = () => 
//GetUnconfirmedGuests = () =>

  render() {
    return (
      <div className ="App">
      <header>
        <h1>RSVP</h1>
        <p>A Treehouse App</p>
        <form>
            <input type="text" value="Safia" placeholder="Invite Someone"/>
            <button type="submit" name="submit" value="submit">Submit</button>
        </form>
      </header>
      <div className ="main">
        <div>
          <h2>Invitees</h2>
          <label>
            <input type="checkbox"/> Hide those who haven't responded
          </label>
        </div>
        <table className ="counter">
          <tbody>
            <tr>
              <td>Attending:</td>
              <td>2</td>
            </tr>
            <tr>
              <td>Unconfirmed:</td>
              <td>1</td>
            </tr>
            <tr>
              <td>Total:</td>
              <td>3</td>
            </tr>
          </tbody>
        </table>

       <Guestlist 
      guests={this.state.guests}
      ToggleConfirmationAt = {this.ToggleConfirmationAt}/>
      </div>
    </div> 
    );
  }
}

export default App;

Any ideas as to why this is occurring? Any help would be appreciated.

1 Answer

you need to deconstruct the old object. what you doing is you are overwriting each object in the "guests" array with a new object that has only the "isConfirmed" property and not copying everything from the old obj. In the toggleConfirmationAt function, you need to deconstruct the "guests" property from the old state.

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

or

toggelConfirmationAt = indexToChange =>(
    this.setState({ guests:this.state.guests.map( (guest, index) => (
        index === indexToChange ? {...guest, isConfirmed: !isConfirmed} :guest )
        })
     )