Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting key prop warning in React, even though key is set

Problem

I'm getting this warning:

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of EventsTable. See fb.me/react-warning-keys for more information.

react-runtime-dev.js?8fefd85d334323f8baa58410bac59b2a7f426ea7:21998 Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of Event. See fb.me/react-warning-keys for more information.

Source

This is EventsTable:

EventsTable = React.createClass({   displayName: 'EventsTable',    render() {     console.dir(this.props.list);      return (       <table className="events-table">         <thead>           <tr>             {_.keys(this.props.list[0]).map(function (key) {               if (key !== 'attributes') {                 return <th>{key}</th>;               }             })}          </tr>         </thead>          <tbody>           {this.props.list.map(function (row) {             return (               <Event key={row.WhatId} data={row} />             );           })}         </tbody>       </table>     )   } }); 

This is Event:

Event = React.createClass({   displayName: 'Event',    render() {     return (       <tr>         {_.keys(this.props.data).map((x) => {           if (x !== 'attributes')             return <td>{this.props.data[x]}</td>;         })}       </tr>     )   } }); 

Question

Clearly I've got the key prop on the <Event /> component. And I'm following the convention that you're supposed to include key on the component, not on the HTML itself (in other words, HTML tags within the Event component). Per the official React docs:

The key should always be supplied directly to the components in the array, not to the container HTML child of each component in the array:

I'm severely confused. Why am I getting warnings?

like image 803
ffxsam Avatar asked Aug 27 '15 18:08

ffxsam


People also ask

What can you do to fix the warning related to React keys?

The easiest fix for this is to create a separate component for the items you're mapping and add the key to that component. Create a new component above your existing component (or link to it your call).

How do you resolve warning each child in a list should have a unique key prop?

The Solution. When creating a list in the UI from an array with JSX, you should add a key prop to each child and to any of its' children. React uses the key prop create a relationship between the component and the DOM element.

Can I use key as a prop in React?

Keys are internal to React and can not be accessed from inside of the component like props.

What does the key prop do in React?

React's key prop gives you the ability to control component instances. Each time React renders your components, it's calling your functions to retrieve the new React elements that it uses to update the DOM. If you return the same element types, it keeps those components/DOM nodes around, even if all the props changed.


2 Answers

Have you tried adding a key to the <th> tag?

         <tr>             {_.keys(this.props.list[0]).map(function (key) {               if (key !== 'attributes') {                 return <th key={key}>{key}</th>;               }             })}          </tr> 
like image 90
sma Avatar answered Oct 07 '22 18:10

sma


I realize this thread is pretty old, but I was just having this issue and it was driving me crazy. I ended up solving it when I realized because I had a <React.Fragment> which also needs a unique key.

like image 40
Michael Aaron Wilson Avatar answered Oct 07 '22 18:10

Michael Aaron Wilson