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) Introducing Props Iterating and Rendering with map()

yoav green
yoav green
8,611 Points

i'm keep getting a message in the console

react.development.js:316 Warning: Each child in an array or iterator should have a unique "key" prop.

Check the render method of App. See https://fb.me/react-warning-keys for more information. in Player (created by App) in App

1 Answer

Cheo R
Cheo R
37,150 Points

The key prop's just react's way to keep track of elements when iterating over them.

You can use the element's index (from the array it's in), but since items can be added/removed from a list, it's better to use something more unique. The key's are for react's internal use.

const food = ['tacos', 'salsa', 'tamarindo', 'torta'];

  <ul>
    {
      food.map((item, index) => <Food key={item.name} obj={item}>);
    }
  </ul>

Where you can replace item.name with index.