Assigning keys to the list
You can assign the array indexes as keys to the list items. The below example assigns array indexes as keys to the elements.
Syntax:
const numbers = [1, 2, 3, 4, 5];
const updatedNums = numbers.map((number, index) =>
<li key={index}>
{number}
</li>
);
ReactJS Keys
React JS keys are a way of providing a unique identity to each item while creating the React JS Lists so that React can identify the element to be processed.
Table of Content
- What is a key in React ?
- Assigning keys to the list
- Issue with index as keys
- Difference between keys and props in React
- Using Keys with Components
- Incorrect usage of keys
- Correct usage of keys
- Uniqueness of Keys
Contact Us