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 Removing Names From the List

Humberto Ventura
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Humberto Ventura
Front End Web Development Techdegree Graduate 14,734 Points

Why do I get TypeError: x is not a function? I

I did everything as Gui says in the video, tried the steps to add a remove name function and I got this error everytime the handler calls the function.Why?

Antti Lylander
Antti Lylander
9,673 Points

Please, share your code. It's impossible to say without it. :)

Humberto Ventura
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Humberto Ventura
Front End Web Development Techdegree Graduate 14,734 Points

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

// component in guest list removeGuest = {this.state.removeGuest }

(GuestList.js)

// handler handleRemove ={() => props.removeGuest(index)} --THIS IS THE LINE OF CODE THE COMPILER SAYS THE ERROR IS, IT SAYS removeGuest() IS NOT A FUNCTION

// In proptypes removeGuest: PropTypes.func.IsRequired,

(Guest.js)

<button onClick = {props.handleRemove}>remove</button>

//proptypes handleRemove:PropTypes.func.isRequired,

Thanks!

1 Answer

you have made a mistake when passing "removeGuest" to "GuestList" component

// This is wrong
 removeGuest = {this.state.removeGuest }
// This is correct
 removeGuest = {this.removeGuest }

you could also implement "removeGuestAt" like this which is a little better

  // remove user
  removeGuestAt = index => this.setState({
    guests:[
      ...this.state.guests.filter( (element, idx) => index !== idx)  
    ]
  });