React newbie here. Im referring this and following traversy react videos and i have customized my app with bootstrap4.5. I understand unique key must be passed for performance and help React identify changed items. I have Users.js
1 const Users = (props) => {
2 //class changed to arrow functions
3 if (props.loading) {
4 return <Spinner />
5 }
6 else {
7 return (
8 <div className="row">
9 {props.users.map((user) => (
10 <div className="col-sm-3 mt-3" >
11 <UserItem key={user.id} user={user} />
12 </div>
13 ))}
14 </div >
15 );
16 }
17 }
in above code i get the error in console below:
Warning: Each child in a list should have a unique "key" prop. Check the render method of `Users`. See https://reactjs.org/docs/lists-and-keys.html#keys for more information.
if i change lines 10 and 11 like below, the error is no longer there
10 <div key={user.id} className="col-sm-3 mt-3" >
11 <UserItem user={user} />
Whats the reason behind this and is it fine to pass key like this since it works ?
The list looks like below which is passed in props
[
{
"login": "mojombo",
"id": 17,
"avatar_url": "https://avatars0.githubusercontent.com/u/1?v=4",
"url": "https://api.github.com/users/mojombo",
"html_url": "https://github.com/mojombo"
},
{
"login": "wintersword",
"id": 18,
"avatar_url": "https://avatars0.githubusercontent.com/u/1?v=4",
"url": "https://api.github.com/users/mojombo",
"html_url": "https://github.com/mojombo"
}
]
react version : "16.13.1"