I'm quite new to react and I'm facing a problem I can't solve. Here is my react component:
import React from 'react';
import Header from './Header';
import ContestPreview from './ContestPreview';
class App extends React.Component {
state = {
pageHeader: 'Naming Contests',
whatever: 'test'
};
render() {
return (
<div className="App">
<Header message={this.state.pageHeader} />
<div>
{this.props.contests.map(contest =>
<ContestPreview key={contest.id} {...contest} />
)}
</div>
</div>
);
}
}
export default App;
This Code gives me the following warning:
Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of
App
. See HERE-FB-URL-I-REMOVED for more information. in ContestPreview (created by App) in App
I totally understand what this error means. I understand that each element of an array should have a unique key when looping trough them. What I don't understand is why I get the error, since in my opionion the key should be defined with my <ContestPreview key={contest.id} {...contest} />
... is key={contest.id}
not enough?
Here is my contest data:
{
"contests": [
{
"id": 1,
"categoryName": "Business/Company",
"contestName": "Cognitive Building Bricks"
},
{
"id": 2,
"categoryName": "Magazine/Newsletter",
"contestName": "Educating people about sustainable food production"
},
{
"id": 3,
"categoryName": "Software Component",
"contestName": "Big Data Analytics for Cash Circulation"
},
{
"id": 4,
"categoryName": "Website",
"contestName": "Free programming books"
}
]
}
In my opionion it should work, I can't understand why I still get this error. I would really like to get some help on my problem, it would be really cool if someone can explain me whats going on here since I really try to understand how it works.
Thank you for your help! :)