Introduction
React lists and keys are among the most basic concepts. This may be the most troubling step for beginners who are just getting started with the React framework. What's scarier is that you can't avoid using lists because practically every application has repetitive content.
But, in practical terms, react lists and keys are pretty simple. All that is required is for it to be conveyed properly. Lists are an essential element for any application. Lists are used in almost every application in some way or another. You may have a task list similar to a calendar app, a photo list similar to Instagram, a shopping cart list, and so on. There are several applications. Lists in an application might be resource-intensive. Imagine an app with a vast list of videos or photos, and as you scroll, you keep getting hundreds more. This could have a negative impact on the app's performance.
Because performance is crucial, you should ensure that any lists you use are built to be as efficient as possible.
Did you know that when using lists in React, each list item needs its own unique key? Let's learn more about React lists and keys, as well as how to use them correctly.
Click on the following link to read further: Hooks in React JS
For a deeper understanding of these concepts, you can explore our React Js Course.
React Lists
Almost every project I've ever worked on included a list of elements. React also makes it a lot easier to render lists in JSX by supporting the Javascript .map() technique.
In Javascript, the .map() method iterates through the parent array, calling a function on each element. Then it creates a new array containing the values that have been changed. It does not affect the parent array.
React Keys
One more thing is required when establishing a list of elements, and that is a key.
The element's key is a specific property that must be included in the element, and it must be a string. Each list's key should be distinct, which means you shouldn't use values that are identical to the key.
Each item within an array must have a unique key, but the key need not be globally unique. The same key can be used across various unrelated components and lists. To put it differently, keys should be unique among siblings rather than globally.
For example, you should not use status or text as a key because they are not identifiers.
The element's key serves as a form of identifier for React, allowing it to figure out which element was updated, added, or removed.
It's a good idea to pick a value that serves as a unique identifier for each item in the array, which is usually the ID.
Creating a basic List component
Lists are commonly used to show lists on websites and are used to display data in an ordered way. Lists can be generated in React in the same way that they are in JavaScript. Let's look at how Lists are transformed in regular JavaScript.
The traversal of lists is done with the map() function.
function ListComponent(props) {
const listItems = myList.map((item) =>
<li>{item}</li>
);
return (
<ul>{listItems}</ul>
);
}
const myList = ["Lotus", "Rose", "Sunflower", "Marigold", "Lily"];
ReactDOM.render(
<ListComponent myList={myList} />,
document.getElementById('root')
);
The code above shows a ListComponent that renders a list of props provided to it. We called the ListComponent in the render() method and handed it a list called myList as props. The following is the outcome of this code:
Lotus
Rose
Sunflower
Marigold
Lily
When you run this code, we'll notice that React issues a warning.
| "Warning: Each child in an array or iterator should have a unique 'key' prop.%s%s See https://fb.me/react-warning-keys for further information.%s" |
It's important to note that the caution here is about providing a unique key. Let us know how to use keys to increase the performance of your React application.





