Keys in react
suggest changeVersions
Keys have been part of React since the early releases covered here (0.3 through 15.5).
Introduction
Keys in React are used to identify a list of elements from the same parent.
If you are iterating over an array to show a list of li elements, each li needs a unique identifier in the key prop. This can usually be the id of your database item. Use the array index only as a last resort.
Remarks
Using the array index as a key is generally not recommended when the array is going to change over time. From the React Docs:
As a last resort, you can pass item’s index in the array as a key. This can work well if the items are never reordered, but reorders will be slow.
A good example about this: https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents