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 trialSam Gord
14,084 Pointscheck my solution and tell if theres any disadvantage using the method i used,
for creating a function to delete Guest what i did was to filter the guests array like this -->
deletePlayer = indexToDelete => {
this.setState({
players: this.state.players.filter((player, index) => {
if (indexToDelete !== index) {
return player;
}
return null;
})
})
}
instead of using two slices like what the teacher did, can you detect any problems with my code? thanks so much
2 Answers
Steven Parker
231,248 PointsHave you tested this thoroughly? the MDN page recommends that arrow function expressions "... are best suited for non-method functions," probably because of the fact they don't establish "this" like conventional functions do. So your references to "this" will bind to whatever it represents in the outer context.
Sean Paulson
6,669 PointsIf you were using hooks instead of classes you would not have to worry about 'this' and it would be fine. Also you can remove the if statement
players: this.state.players.filter((player, index) => indexToDelete !== index)