Skip to main content

Keys for React Elements

In JSX loops don't use random values such as Math.random() or the item index as the key of the item.

If you're filtering an array and using Array.map() to create UI elements, if the filter condition changes, you might get the same elements but with different indices. React will see it as a brand-new item because the key has changed. Always use a value that is unique to the element, such as a unique ID from database.