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 Basics (2018) Understanding State Remove Items From State

Why does he passes a id prop to the Player component if there's already a key prop that could be used instead?

Couldn't the Player's key prop be used as an id?

<Player 
  name={player.name}
  id={player.id}
  key={player.id.toString()}
  removePlayer={this.handleRemovePlayer}
/>
Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,004 Points

Hi Ewerton. This just showed back up in my que today. Is this something you still need help with? I am not familiar with react but will look into if you are still stuck.

1 Answer

The key is a special string attribute help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity. They're not passed down to children as usable values.

If you try to console.log(props.key) for example, you'll see that it's undefined. Meaning {() => props.removePlayer(props.key)} won't work.