Try using payload[i].dataKey
or payload[i].name
if you know that it will be unique across your items. Otherwise if the order will not change between renders (you don't change the array by adding/removing items or sorting it), you can use i
as the key.
You were using payload[i]
which translates to [object Object]
as a string, for all the elements (Object.toString()
will return [object Object]
) -- so it was not unique.
This is needed for the reconciliation algorithm of React Virtual DOM (when having multiple nodes, you need a unique key for them so it will not update all of them when only one changes).
When children have keys, React uses the key to match children in the original tree with children in the subsequent tree.
In practice, finding a key is usually not hard. The element you are going to display may already have a unique ID, so the key can just come from your data:
When that’s not the case, you can add a new ID property to your model or hash some parts of the content to generate a key. The key only has to be unique among its siblings, not globally unique.
As a last resort, you can pass an item’s index in the array as a key. This can work well if the items are never reordered, but reorders will be slow.
Reorders can also cause issues with component state when indexes are used as keys. Component instances are updated and reused based on their key. If the key is an index, moving an item changes it. As a result, component state for things like uncontrolled inputs can get mixed up and updated in unexpected ways.