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

Robert Stamate
Robert Stamate
13,227 Points

Can we use the second value (the index) passed into a .map(item, index) function as the key?

So the .map() function accepts more to it: players.map((item, index) => <Player key={index} name={item.name} score={item.score}) />

Is it safer to add another key to each item in the array?

1 Answer

Using id as a key is an anti pattern in React. You should rather use a unique id as the key, for example the id of the player or if you don't have such a thing, you should prapere a "player" + i string and pass it as a key. I hope it helps!