ReactJS

ReactJS | Lists and Keys Tutorial with Example

Hello ReactJS Friends,

Lists and Keys in React are the most common concepts used for almost every ReactJS Application. Let’s learn about lists and Keys in ReactJS with examples.

Lists and Keys in ReactJS

A “key” is a special string attribute. Keys must be included when creating lists of elements in React.

Uses of Keys in ReactJS

  • Keys are used in React to identify which items have been changed, updated, or deleted from the list.
  • It also helps to determine which components need to be rerendered instead of rerendering the whole set of components.
  • Keys are used to give an identity to the elements in the lists.

The next thing that comes to mind is what should be good to be chosen as the key for the items in the lists. Using a string as a key that uniquely identifies the items in the list is recommended.

Example:

The below example shows a list with string keys.

const numbers = [ 1, 2, 3, 4, 5 ];
 
const updatedNums = numbers.map((number)=>{
return <li key={index}>{number} </li>;
})

We can also assign the array indexes as keys to the list items.

const numbers = [ 1, 2, 3, 4, 5 ];
 
const updatedNums = numbers.map((number, index)=>
<li key = {index}>
{number}
</li>
);

Output:

When we set the key in any tag with the ‘key’ keyword, the error of the key prop is gone.

function App()
{
    const menuItems = [1, 2, 3, 4, 5];
    return (
        <div className="App">
        <header className="App-header">
        {
            menuItems.map(el => {
                return <div key={el}>{el}</div>
            })
        }
        </header>
        </div>
    );
}

Output:

Conclusion:

This was all about Lists and Keys in ReactJS. Hope the examples mentioned above will help you better understand Lists and Keys in React. Hire ReactJS Developers for all your development needs.

Share the tutorial with your other RectJS friends, and stay in touch with us for more tutorials on ReactJS.

Happy Coding!

Click to rate this post!
[Total: 4 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago