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 Creating the Counter

Peter Gess
Peter Gess
16,553 Points

Nothing happens when clicking the "remove" button

Even though I have passed totalInvited as props I am not seeing anything fire when clicking "remove" like he is in the video.

1 Answer

Peter Gess
Peter Gess
16,553 Points

Here is my app.js, let me know if you need counter:

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

class App extends Component {

  state = {
    isFiltered: false,
    pendingGuest: '',
    guests: [
      {
        name: 'Peter',
        isConfirmed: false,
        isEditing: false
      },
      {
        name: 'Pat',
        isConfirmed: true,
        isEditing: false
      },
      {
        name: 'Luke',
        isConfirmed: true,
        isEditing: true
      }
    ]
  }

  toggleGuestPropertyAt = (property, indexToChange) =>
    this.setState({
      guests: this.state.guests.map((guest, index) => {
        if (index === indexToChange) {
          return {
            ...guest,
           [property]: !guest[property]
          };
        }
        return guest;
      })
    });

  toggleConfirmationAt = index =>
    this.toggleGuestPropertyAt('isConfirmed', index);

  removeGuestAt = index => 
    this.setState({
      guests: [
        ...this.state.guests.slice(0, index),
        ...this.state.guests.slice(index + 1)
      ]
    })

  toggleEditingAt = index =>
    this.toggleGuestPropertyAt('isEditing', index); 

  setNameAt = (name, indexToChange) =>
    this.setState({
      guests: this.state.guests.map((guest, index) => {
        if (index === indexToChange) {
          return {
            ...guest,
           name
          };
        }
        return guest;
      })
    });

  toggleFilter = () =>
    this.setState({ isFiltered: !this.state.isFiltered })

  handleNameInput = e => 
    this.setState({ pendingGuest: e.target.value });

  newGuestSubmitHandler = e => {
    e.preventDefault();
    this.setState({ guests: [
       {
        name: this.state.pendingGuest,
        isConfirmed: false,
        isEditing: false
        },
         ...this.state.guests
      ],
      pendingGuest: ''
    });
  } 


  getTotalInvited = () => this.state.guests.length;
  // getAttendingGuests = () =>
  // getUnconfirmedGuests = () =>

  render() {
  const totalInvited = this.getTotalInvited();
    return (
      <div className="App">
      <header>
        <h1>RSVP</h1>
        <p>A Treehouse App</p>
        <form onSubmit={this.newGuestSubmitHandler}>
            <input
             type="text"
             onChange={this.handleNameInput}
             value={this.state.pendingGuest} 
             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"
            onChange={this.toggleFilter}
            checked={this.state.isFiltered} /> Hide those who haven't responded
          </label>
        </div>
        <Counter
          totalInvited={totalInvited} />

       <GuestList 
       guests={this.state.guests}
       toggleConfirmationAt={this.toggleConfirmationAt} 
       toggleEditingAt={this.toggleEditingAt}
       setNameAt={this.setNameAt}
       isFiltered={this.state.isFiltered}
       removeGuestAt={this.state.removeGuestAt}
       pendingGuest={this.state.pendingGuest}
       />

      </div>
    </div>
    );
  }
}

export default App;