<MenuList width={'148px'} >
{ Object.entries(userRoleMenu[UserRole]).map((item, index) => <>
{item[0]==='fpv?
<MenuItem _hover={{backgroundColor:'gray.50'}} key={item[0]} onClick={handleMenuClick(item)}>
</MenuItem>
:
<MenuItem _hover={{backgroundColor:'gray.50'}} key={item[0]} onClick={handleMenuClick(item)}>{item[0]}</MenuItem>
}
</>)
}
</MenuList>
error:
UserProfileThumbnailMenuList.tsx:65 Warning: Each child in a list should have a unique "key" prop.
Check the render method of UserProfileThumbnailMenuList
. See https://reactjs.org/link/warning-keys for more information.
at UserProfileThumbnailMenuList
code is in UserProfileThumbnailMenuList
i dont know why unique key error still exist
3
Answers
I’ve added the correct key prop to the React.Fragment, and I’ve also fixed the syntax error in the conditional check by changing {item[0]===’fpv? to {item[0]===’fpv’ ?. Make sure to replace the placeholder comment with the actual content for the fpv case inside the MenuItem.
This error is occur because you use
<> ... </>
react fragment in this way which react don’t apply key on it.You should do this instead:
In your case, your
key
is not unique, since you.map
and set the same key for every element created.You need to change
key={item[0]}
tokey={index}
, or if youritem
element is an object and has a unique identifier, like anid
, you could do something likekey={item.id}
Also, you don’t need to specify the element and the index
item[0]
, while theitem
contains the exactly element index of the array returned fromObject.entries(userRoleMenu[UserRole])
sincemap
take care of it.Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
You should make the following changes: