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 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,686 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)  
    ]
  });