DEV Community

SavvyShivam
SavvyShivam

Posted on

2 1

Keys in React

Keys in React

Introduction to Keys

A “key” is a special string attribute you need to include when creating lists of elements in React. Keys are used in React to uniquely identify which items in the list are changed, updated, or deleted. In other words, we can say that keys are used to give an identity to the elements in the lists. It is recommended to use a string as a key that uniquely identifies the items in the list.

Below is the updated code with keys:

This code will give the same output as that of the previous code(demonstrated in the List module) but this time without any warning. Keys are the most useful when we create components dynamically or when there is alteration of any sort in the list.

Using Keys in Components

Consider a situation where you have created a separate component for list items and you are extracting list items from that component. In that case, you will have to assign keys to the component you are returning from the iterator and not to the list items. That is you should assign keys to and not to “li” tag. A good practice to avoid mistakes is to keep in mind that anything you are returning from inside of the map() function is needed to be assigned a key.

MenuItems is the component to be extracted.

Navmenu is the component that will return an unordered list.

Output

The code above can be found in the link given below:

https://codesandbox.io/s/keys-in-comp-qbuoks

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay