Key Prop for Lists in React JS 🎯

beginner
10 min

Key Prop for Lists in React JS 🎯

Welcome to the Key Prop for Lists tutorial! In this lesson, we'll explore how to create dynamic lists in React JS using the map function and the key prop. By the end of this tutorial, you'll be able to create, update, and delete list items with ease. 📝

What are Lists in React JS? 📝

Lists in React JS are groups of elements that are rendered based on an array. They can be used to display data such as lists of items, data tables, or even navigation menus.

The Importance of the key Prop 💡

The key prop is a mandatory attribute for list items in React JS. It helps React identify which items have changed, are added, or are removed, allowing it to update the list efficiently and avoiding performance issues.

Creating a Simple List 🎯

Let's start by creating a simple list of items.

jsx
import React from 'react'; const MyList = () => { const items = ['Apple', 'Banana', 'Orange']; return ( <ul> {items.map((item, index) => ( <li key={index}>{item}</li> ))} </ul> ); }; export default MyList;

In the example above, we have an array of items and use the map function to loop through them and create list items. The key prop is set to the index of the item in the array, a common approach for simplicity.

Proper use of the key Prop 💡

While using the index as a key can work for simple lists, it's not always the best approach. When the order of the items in the array changes, the index might not uniquely identify each item, causing performance issues. Instead, it's a good practice to use a unique identifier for each item as the key.

jsx
const MyList = () => { const items = [ {id: 1, name: 'Apple'}, {id: 2, name: 'Banana'}, {id: 3, name: 'Orange'} ]; return ( <ul> {items.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> ); }; export default MyList;

In this example, we have an array of objects, each with a unique id. Using this id as the key ensures that React can efficiently update the list when items are added, removed, or reordered.

List Quiz 🎯

Quick Quiz
Question 1 of 1

What is the purpose of the `key` prop in React JS lists?

Updating and Deleting List Items 💡

To update or delete list items, you can pass a function to each item as a prop, such as onClick or onDoubleClick. In the function, you can perform the necessary actions and update the underlying data.

jsx
const MyList = () => { const [items, setItems] = React.useState([ {id: 1, name: 'Apple'}, {id: 2, name: 'Banana'}, {id: 3, name: 'Orange'} ]); const handleClick = (id) => { setItems(items.filter(item => item.id !== id)); }; return ( <ul> {items.map(item => ( <li key={item.id} onClick={() => handleClick(item.id)}>{item.name}</li> ))} </ul> ); }; export default MyList;

In this example, we have a stateful component that maintains the list of items. When an item is clicked, the handleClick function is called, and the list is updated by filtering out the clicked item.

Wrapping Up ✅

In this tutorial, you learned about the key prop for lists in React JS, its importance, and how to create, update, and delete list items. By understanding the key prop, you can create efficient and dynamic lists that can be used in various real-world projects.

Keep practicing, and happy coding! 🎉