Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Basics 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.