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.
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> ) } });
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?
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).
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.
Keys are internal to React and can not be accessed from inside of the component like props.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With