-
Notifications
You must be signed in to change notification settings - Fork 45.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to eliminate 'Each child in a list should have a unique "key" prop' warning. #15620
Comments
You have to pass the |
^ like @thomasheyenbrock said, the root element in the array should have a key prop. Further, you should probably give a key based on your item's id, or you'll risk a bug of the list gets reordered. Rewriting your component (and removing another unnecessary Fragment) - import React from "react";
import { List, ListItem, ListItemText } from "@material-ui/core";
const data = [
{
id: "1",
caption: "Index"
},
{
id: "2",
caption: "About"
}
// ...
];
class BWKNav extends React.Component {
render() {
return (
<List component="nav">
{data &&
data.length > 0 &&
data.map((item1, index1) => {
return (
<React.Fragment key={item1.id}>
<ListItem divider={true}>
<ListItemText primary={item1.caption} />
</ListItem>
{/* Here loop secondary menu, Even if I only run the current code, this warning will still appear */}
</React.Fragment>
);
})}
</List>
);
}
} |
####### I also face same problem... please help... ######## import React, {Component} from 'react'; export class Authors extends Component{
} |
Hope someday the key needn't added by developers. |
Don't understand @yanlee26 |
This didn't work for me.
|
Having the same problem. Any suggestions? |
I found the solution here: Every record (your rows) also need a "key" |
I believe that worked. I removed the Thanks |
I'm having a similar issue but I have the key prop. So I'm unsure of what it's complaining about
|
Every item of vocabList needs to have a "key" prop as well. See my comment above. |
I believe adding the key to |
the warning and stacktrace are not very helpful in locating the actual line of code that is buggy. would appreciate some more precise info that will allow me to zero in on the bug |
I just spent far too long to find out that it wasn't the I agree we need better error handling around this. I spent far too long thinking it must be some weird bug with passing |
I have this problem and adding the key to each item doesn't matter. Add to this; there's only one item! So...why would it yell at me about uniqueness? `import React from "react"; import ContextButton from "../global/context_button"; import "./scss/experience.scss"; const Experience = props => {
}; export default Experience;` Output: |
As and when i see this error in console. i am happy to resolve it by adding key attribute. but a simple problem is it never tells that which entity/element is missing it. i tried adding keys all over the loops and even non-loops but the error not getting off. Can you put just a simple element name which requires the key in error message? Thanks. |
Surprising that this is still a snag in React development coming up on the year 2022. It would save a lot of developer pain if the warning could offer some information about the elements with missing keys. You can approximate this if you enable "Break on Warnings" in React Dev Tools and look through the stack trace around the In my case, I had a leftover fragment |
In my case it was a function that used a spread for the props, so this didnt work <div {...spreadProps()} key={id} /> this did const { key, ...partialSpreadProps } = spreadProps();
<div {...partialSpreadProps} key={id} /> |
Thank you! |
Thanks. This works for me. But if you remove the li parent, it works. |
Do you want to request a feature or report a bug?
Feature / Bug
What is the current behavior?
I am use Lists(Material-UI components) render nav. But 'Each child in a list should have a unique "key" prop' warning always appears.
I am pretty sure that the key attribute has been added for List and ListItem under each map, and the value is unique.
This is code:
What is the expected behavior?
Eliminate warnings.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
latest.
The text was updated successfully, but these errors were encountered: