by mediumdeviation · 3y ago · 0 comments
Giving every item a new key on every update does not help with that - in fact it is likely strictly worse than just using index since it will cause every item to re-mount every update https://beta.reactjs.org/learn/preserving-and-resetting-stat... which is unlikely to be what you want.
React can’t see the difference between a reorder and remove&insert. When reordering items the state should be moved as well; when removing and inserting a new item, state should reset.
Using an array index is equivalent to silencing the error
It depends on what your data source is. If it’s a stable sorted list that only ever gets appended at the end, you can use the index.
At the end of the day, there is no hard fast rule for the key. You have to know what your data source is.
You have to know what you’re doing.
fwiw the author specifies says not to do this. The advice is to generate a unique ID when items are created, not when they’re rendered.