博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The impacts of using index as key in React
阅读量:6624 次
发布时间:2019-06-25

本文共 2336 字,大约阅读时间需要 7 分钟。

Let's say there's a list that you want to show in React, and some developers may use index as key to avoid the warning of React, like this:

    {list.map((item, index) =>
  • {item.name}
  • )}
复制代码

Sure, you can do that, but it's a bad idea. Let's see what official said:

We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state.

But how does the key work? Don't worry, this is what I want to talk about next.

Example

Let's start with a example to figure out what the difference between using index and using unique id.

.

we render 2 different list initially and every item has a uncontrollable input. There are also some buttons on top which we can insert or delete items, each new item will be colored.

  • push: insert a item at the end of list
  • unshift: insert a item at the start of list
  • spliceInsert: insert a item at the middle of list

The left side represents list with index-key, the right side represents list with unique-key.

As we can see from pictures, there seems to be something wrong on the left side. It got confused about which item belonged to which dom.

Analyze

It will reuse the dom that already exist if their key are the same, that's the role of key. For more detail, is the source code.

For the sake of understanding, I'll explain it in a case.

An array of length 5, I want to insert an item in 3rd position. when it comes to index of [0, 1], it's okay to reuse the existing doms. But when it comes to index of 2, it also reuses the existing dom because of same key. Besides, their state is different so the children of dom[2] will be updated. Next, index of 3 reuses dom[4] and so on.

It may cause terrible performance if list is long enough. If you insert a item at the start of list, it'll insert a dom at the end and update children all of item. But with unique id, it just insert a dom at the start.

Conclusion

It's a bad idea to use the array index since it doesn't uniquely identify your elements. In cases where the array is sorted or an element is added to the beginning of the array, the index will be changed even though the element representing that index may be the same. This results in unnecessary renders.

If you have to use index as key, please make sure you only operate the last item.

转载地址:http://citpo.baihongyu.com/

你可能感兴趣的文章
转:OpenResty最佳实践(推荐了解lua语法)
查看>>
转:CEO, CFO, CIO, CTO, CSO是什么
查看>>
andriod自定义视图
查看>>
linux下vim更改注释颜色
查看>>
在SSL / https下托管SignalR
查看>>
Using JRuby with Maven
查看>>
poj 3308 (最大流)
查看>>
Netty了解与小试
查看>>
醒醒吧少年,只用Cucumber不能帮助你BDD
查看>>
一名女程序员对iOS的想法
查看>>
西班牙现新型电费退款网络诈骗 侨胞需谨防上当
查看>>
ArrayList
查看>>
Angular学习笔记(一) - 之安装教程
查看>>
Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)...
查看>>
最严新规发布 网络短视频平台该如何降低违规风险? ...
查看>>
云服务器ECS出现速度变慢 以及突然断开怎么办?
查看>>
208亿背后的“秘密”
查看>>
Android系统自带样式(android:theme)解析
查看>>
全志A33开发板Linux内核定时器编程
查看>>
全栈必备 敏捷估点
查看>>