What is indexing of list? | Avoid using index as keys

KnowledgeKeen
28 May 202209:35

Summary

TLDRThis video delves into the pitfalls of using indexes as keys in list rendering, a common practice in programming. It explains why indexes should not be used due to their tendency to reset upon any list manipulation such as sorting, adding, or removing elements, which can lead to confusion and errors. The video suggests using unique identifiers or a combination of attributes instead to maintain stability. It also outlines scenarios where using an index might be acceptable, such as in static lists. The presenter emphasizes the importance of adhering to best practices and references React documentation, which advises against using indexes as keys.

Takeaways

  • 📚 Never use index as a key in lists because it can lead to complications when the list is manipulated.
  • 🔑 Keys should be unique and not based on the index value to maintain consistency.
  • 🎯 Create a combination of identifiers for keys to ensure uniqueness, such as combining a string with an index.
  • 🛑 Avoid using index as a key when the list is subject to dynamic changes like sorting, adding, or removing elements.
  • 🔄 Index values are reassigned whenever the list is re-rendered, which can cause issues with list integrity.
  • 🍏 Demonstrated an example where adding an element to the beginning of a list caused all indexes to reset.
  • 📈 Provided a practical example to illustrate the problems that can arise from using indexes as keys.
  • 🚫 React documentation advises against using indexes as keys due to potential issues with list manipulation.
  • ✅ Use indexes as keys only in static scenarios where the list is not expected to change.
  • 🔑 When a unique attribute like 'id' is available, it should be used instead of the index for keys.
  • 💡 Encourages viewers to comment with questions or additional insights to further the discussion on the topic.

Q & A

  • Why should we avoid using index as a key in list rendering?

    -Using index as a key can lead to complications when the list is manipulated (e.g., items added or removed), as the indexes will be reassigned and cause the entire list to re-render, which can lead to performance issues and incorrect behavior.

  • What is the recommended way to create unique keys for list items?

    -It is recommended to use unique values such as IDs or a combination of attributes (like name and color) to create unique keys for list items, ensuring that each item can be uniquely identified even when the list is reordered or items are added or removed.

  • What happens when a new element is added to the beginning of a list with index keys?

    -When a new element is added to the beginning of a list that uses index keys, the entire list gets re-rendered with new index values, causing the original items to shift in their positions and potentially leading to incorrect rendering.

  • Can index keys be used in any situation?

    -Index keys can be used in very basic scenarios where the list is static and there is no possibility of the list being sorted, having items added or removed, or manipulated in any way.

  • What are some scenarios where using index as a key is not advisable?

    -Using index as a key is not advisable when there are unique values available, when the list is expected to be sorted, or when there will be dynamic addition, deletion, or manipulation of the list items.

  • What does the video suggest as an alternative to using index as a key?

    -The video suggests using a combination of attributes or unique identifiers (like IDs) as an alternative to using index as a key to avoid the issues associated with list manipulation.

  • How does the video demonstrate the problem with using index as a key?

    -The video demonstrates the problem by adding a new element to the beginning of a list that uses index as a key, showing how the indexes are reset and the entire list is re-rendered, leading to incorrect item positions.

  • What is the default behavior when a list with index keys is manipulated?

    -The default behavior when a list with index keys is manipulated is that the entire list gets re-rendered with new index values, which can cause performance issues and incorrect item positioning.

  • Why is it important to have unique keys for list items in React?

    -Unique keys are important in React to help the framework identify which items have changed, are added, or are removed. This allows React to efficiently update the DOM and improve performance.

  • What does the React documentation say about using index as a key?

    -The React documentation advises to avoid using index as a key and suggests using unique identifiers instead, as it can lead to unpredictable results and performance issues when the list is manipulated.

Outlines

00:00

🚫 Avoiding Index as Keys in Lists

The video begins by addressing the common practice of using indexes as keys in list rendering, a method that is strongly discouraged. The speaker explains that while rendering lists with keys as indexes might seem straightforward, it can lead to complications. To illustrate this, an example is provided where a list of fruits is rendered with index keys. The video demonstrates the pitfalls of this approach by showing what happens when a new fruit is inserted at the top of the list: the indexes are reset, leading to a re-rendering of the entire list and incorrect association of data with the wrong indexes. The speaker emphasizes the importance of using unique identifiers or a combination of attributes as keys to avoid such issues and ensure the stability and predictability of the UI.

05:03

📚 Best Practices for Using Indexes in React

Continuing from the previous discussion, the second paragraph delves into the implications of using indexes as keys and the scenarios where it should be avoided. The speaker provides guidance on when it is acceptable to use indexes, such as in static lists where the order is unlikely to change. However, for dynamic lists where items may be added, removed, or sorted, the use of indexes is strongly advised against. The video highlights the importance of using unique values or a combination of attributes to create unique keys for list items. The speaker also references React's official documentation, which recommends avoiding the use of indexes as keys. The video concludes by encouraging viewers to ask questions or add comments for further discussion, emphasizing the value of community engagement in learning.

Mindmap

Keywords

💡rendering

Rendering refers to the process of generating an image, video, or any visual representation from a model by means of computer programs. In the context of the video, rendering is used to describe how a list is being displayed or visualized in a user interface, such as a web page. The script mentions rendering a list with keys, which is a common practice in programming to ensure that each element in the list can be uniquely identified and managed.

💡keys

In programming, particularly in the context of the video which seems to be about list rendering in a programming environment like React, keys are unique identifiers used to manage and track the items in a list. Keys help the system to determine which items have changed, are added, or are removed, thus optimizing the re-rendering process. The script discusses the use of indexes as keys, which is not recommended.

💡index

An index is a numerical value that represents the position of an item in an ordered collection, such as an array or list. In the video script, the presenter initially uses index values as keys for rendering a list but then explains the pitfalls of this approach. The index is problematic as a key because it can change when items are added or removed from the list, leading to potential bugs and performance issues.

💡unique

Unique in this context means that each key assigned to an item in the list should be distinct and not duplicated. The video emphasizes the importance of having unique keys to prevent conflicts and ensure the correct identification of list items. The script suggests combining the index with other properties, like 'fruit.name' and 'food.color', to create a unique key.

💡implications

Implications refer to the consequences or effects of a particular action or decision. In the video, the implications of using index as a key are explored, demonstrating that it can lead to issues when the list is modified, such as inserting or removing items, because the indexes will be reassigned, causing the system to misidentify the items.

💡state

In the context of React, state refers to a plain JavaScript object that represents the part of the component that can change over time. The script mentions 'state' when discussing how the list of fruits is managed and updated within the component's state. This is crucial for ensuring that the user interface remains in sync with the underlying data.

💡re-rendering

Re-rendering is the process of re-drawing or re-displaying a user interface component or element when its state or props change. The video script uses the term to illustrate what happens when a list is modified and how using indexes as keys can lead to the entire list being re-rendered with new index values, which can be inefficient and lead to errors.

💡dynamically

Dynamically, in the context of this video, refers to the process of changing data or the structure of the list in real-time, such as adding or removing items. The script warns against using indexes as keys when the list is expected to be manipulated dynamically because it can cause the indexes to be reassigned and lead to incorrect rendering.

💡performance

Performance in this context is related to the efficiency and speed at which a program or system operates. The video discusses the impact of using indexes as keys on performance, suggesting that it can lead to unnecessary re-renders and thus affect the overall performance of the application.

💡React

React is a popular JavaScript library used for building user interfaces, particularly single-page applications. It is mentioned in the script as the likely environment in which the list rendering is being discussed. React has specific best practices for managing lists and keys, which the video aims to address.

Highlights

The video discusses the implications of using index as keys in list rendering.

It is advised not to use index as a key due to potential issues with list re-rendering.

A key should ideally be a unique identifier, not an index value.

Combining the index with other properties like name can create a unique key.

The video provides an example of list rendering using fruit names and colors.

Adding a property 'id' to each item in the array to demonstrate the use of unique identifiers.

Inserting a new element at the beginning of the list to illustrate the complications of using index as keys.

When a new element is added, the indexes are reset, causing a re-rendering issue.

Indexes are not stable and get reassigned whenever the list is manipulated.

Using indexes as keys can lead to incorrect associations between data and DOM elements.

The video recommends avoiding index as keys for dynamic data manipulation.

Indexes can be used in very basic scenarios where the list is static and not subject to change.

React documentation advises against using indexes as keys.

The video outlines scenarios where using index as keys should be avoided.

Creating unique keys by combining multiple attributes is suggested for better list management.

The importance of understanding the implications of using index as keys for performance is emphasized.

The video concludes by inviting viewers to ask questions or add more insights in the comments.

Transcripts

play00:00

hey guys

play00:01

another day another video we have seen

play00:04

what uh rendering of list we have seen

play00:07

what are keys and now today we are gonna

play00:09

see

play00:10

what are indexing of this list and why

play00:13

we should avoid using index as keys so

play00:16

without wasting any time let's dive into

play00:18

this video if you're new to this channel

play00:21

what you know what to do you have to

play00:22

subscribe to this channel and give this

play00:24

video a like

play00:25

[Music]

play00:34

so in the previous example we saw that

play00:37

we rendered some list and keys and keys

play00:39

as

play00:40

index right we just rendered a key as an

play00:43

index but

play00:46

a surprise comes that we should never

play00:48

use index as key now why is that we are

play00:52

going to see in this video

play00:54

uh let's start with what are the

play00:56

implications of using index as

play00:59

key okay so the key should not have this

play01:02

index value

play01:04

in real life scenarios what we usually

play01:06

do we just make a simple

play01:09

combination of these keys like something

play01:12

like this so what i'll do i'll just add

play01:15

a string index okay with a combination

play01:19

something like my name as well so fruit

play01:24

oops fruit.name and food.color so this

play01:27

will give you a good combination and it

play01:29

will also make your list

play01:32

keys unique

play01:34

and hence it will not have any kind of

play01:37

implication so what are the implications

play01:39

that we are going

play01:40

what are the implications that we are

play01:41

talking about here are that we should

play01:43

not use index you will see in the

play01:45

following example so let me just modify

play01:48

this example a bit we still have the

play01:50

same apple pear and mango okay as my

play01:55

[Music]

play01:56

values into my array and we are just

play01:58

rendering the same thing we are just

play02:00

simply rendering it 0 1 2 let me quickly

play02:04

add something like a property called as

play02:07

id which is

play02:09

1

play02:12

id 2

play02:14

id 3

play02:16

okay and i'll simply print this

play02:19

id as the fruit

play02:24

dot

play02:25

id

play02:26

you can see over here

play02:28

that

play02:29

now this is an index value this is my id

play02:32

and this is the name and this is the

play02:33

color color right now we don't need it

play02:35

so let's not make more confusing over

play02:37

here we'll just simply remove the color

play02:39

so as to understand

play02:42

this is my index value the first value

play02:44

is index the second value is id the

play02:46

third value is my

play02:47

name

play02:48

so i'll simply add a button

play02:53

on click

play02:55

i'll say

play02:56

insert

play02:58

at top

play03:00

so i'm not going to do anything i'm just

play03:01

simply going to add a

play03:04

hard coded value

play03:06

just to show you what are the

play03:08

complications of using index

play03:12

so before i do that okay this should be

play03:14

something the fruit should be in my

play03:16

state because i'm changing the state

play03:18

right now and i want my uh dom to be

play03:21

updated

play03:22

and doing something like this will never

play03:25

make any changes on my dom and hence

play03:27

what i'll do this is a constant i'll

play03:29

just say simply

play03:32

temporary fruits

play03:34

[Music]

play03:36

a simple constant but i'll create a

play03:38

state

play03:40

state

play03:42

and this would be my fruits

play03:46

fruits

play03:48

use date

play03:49

i

play03:50

we import it from react and the default

play03:53

value should always be my

play03:55

this list

play03:57

if you just see nothing has changed only

play03:59

the add button is

play04:02

here we just haven't simple add button

play04:04

if you want to see what is happening to

play04:05

the ad if that is working or not we'll

play04:08

simply say insert

play04:10

we just added a simple console if you

play04:13

try yes insert is working

play04:15

absolutely beautiful

play04:17

now

play04:18

by clicking on this add button we are

play04:20

just going to add let's say another new

play04:22

element peach or anything at the very

play04:25

beginning of this before apple and let's

play04:28

see what is going to happen to our list

play04:30

what is going to be happening to our

play04:33

indexes the list and everything

play04:36

i'll just create a simple

play04:38

object constant

play04:41

simple object

play04:43

equal to

play04:45

i have a new id that is 4

play04:48

i have a new name so it will be it would

play04:50

be a new fruit like guava or anything

play04:53

and let's say that color would be

play04:56

green

play04:57

this is a simple object i just want to

play04:59

push this object into my fruits

play05:03

so what i will do i'll just say

play05:06

set

play05:07

fruits

play05:09

[Music]

play05:10

the existing fruits whatever existing

play05:12

fruits are there

play05:14

let it be oops

play05:16

we are just going to push it in an array

play05:19

and with that

play05:21

i'll just simply add my simple object

play05:24

i hope this works let's see

play05:29

yes so the new index is here

play05:31

and the id is here and my

play05:34

the new element is added at the

play05:37

end of my

play05:38

list but we want at the end no we just

play05:41

simply want it at the very beginning so

play05:44

i'll just add it at the beginning and

play05:46

then expand the rest of my object this

play05:49

is also going to work let me quickly

play05:50

refresh now when i am going to

play05:54

look at it very carefully

play05:56

these are existing

play05:58

indexes that are already there so 0 is

play06:02

the zero index is for apple one index is

play06:05

for pair two indexes for mango now three

play06:09

even though i'm adding at the very top

play06:11

it should be three at the

play06:13

very beginning which should be given to

play06:15

the

play06:16

but when i click add

play06:19

it breaks

play06:21

there is no error but again the whole

play06:24

indexes is reset

play06:27

zero goes to gawa one goes to apple two

play06:29

goes to pair and three goes to mango

play06:32

this is the reason we should avoid using

play06:36

indexes you see what is happening it is

play06:38

guessing the whole list is getting

play06:40

re-rendered or in short the whole list

play06:42

is getting the new index values okay so

play06:46

gamma is getting like zero apple is

play06:48

getting one pair is getting two mango is

play06:51

getting

play06:52

three even though ids are different

play06:54

irrespective of of whatever the ids are

play06:57

but the indexes are again reset from

play07:00

zero to and so on okay whenever we do

play07:03

something like this or whenever we sort

play07:05

the list whenever we add something

play07:07

whenever we remove something

play07:10

our indexes get reassigned

play07:13

that's the reason we should always and

play07:16

always

play07:17

avoid

play07:19

using

play07:20

index as a key

play07:23

so when should we use it and when we

play07:24

should not so i said that you should

play07:27

avoid using it but you can also use it

play07:29

in very basic scenarios when you know

play07:32

that there is nothing uh your table or

play07:34

your whole data your uh your list is

play07:37

never going to be sorted something is

play07:40

never going to be added dynamically in

play07:42

between the list something is never

play07:44

going to be popped out from the list in

play07:45

between so when these scenarios are

play07:48

there you can use

play07:50

index because it is very simple okay

play07:52

it's a simplest thing

play07:54

but when you know that your data is

play07:56

going to be populated dynamically

play07:57

something is going to come in between

play07:59

the listing is going to be jumbled up

play08:01

sometime in the future then you should

play08:03

always avoid index which is a very good

play08:06

practice overall avoiding index it is

play08:09

itself a good practice

play08:11

even if you go to the react official

play08:13

documentation there it is mentioned that

play08:14

you should try and avoid using indexes

play08:17

so let's see when what are these

play08:20

important things you should not use

play08:21

index in the following scenarios the

play08:23

number one is

play08:25

when you have unique value or at least a

play08:28

combination to create unique values

play08:30

try avoiding index so at the very

play08:32

beginning what we saw we we did a

play08:34

combination of like name and index you

play08:36

can do that it is absolutely perfect it

play08:39

is absolutely fine and it's a very good

play08:41

way when you don't have a unique

play08:44

attribute like i have an id as a unique

play08:46

value but even though when you don't

play08:48

have a unique value in your list you can

play08:50

use some other combinations which makes

play08:52

it more unique

play08:55

the second scenario would be there

play08:57

should not be sorting

play08:59

addition

play09:00

deletion

play09:02

or any kind of manipulation of the list

play09:05

dynamically

play09:06

in the near future in your example

play09:08

then you should avoid using

play09:10

index this was a short video but it was

play09:14

a very important video from the

play09:16

perspective of performance

play09:18

you get something out from it if you

play09:20

have any questions regarding index why

play09:22

again or if you just want to add

play09:24

something more to this video please

play09:25

mention it in the comment if i missed

play09:27

something on the other thing i love your

play09:29

comments and i love replying to them

play09:32

thank you so much i hope this was

play09:34

interesting

Rate This

5.0 / 5 (0 votes)

Related Tags
List RenderingReact KeysUnique KeysIndex AvoidanceDynamic DataPerformance TipsReact Best PracticesJavaScript TutorialDOM UpdatesKey Implications