Comments (2)
元素key属性的作用是用于判断元素是新创建的还是被移动的元素,从而减少不必要的元素渲染。
from react-illustration-series.
重新整理了一下哈, key的作用那篇写的比较草, 现在优化了一下.
key
的作用就是服务于diff算法
, 是节点是否可以复用的首要判定条件.- 如果省略了
key
, 内部会默认使用null
, 在列表节点有排序需求的情况下, 会造成性能损耗.
在多节点的情况下
除非是静态列表渲染, 没有更新动作, 可以省略key.
一般在列表需要二次渲染更新的情况下:
- 加上
key
只有好处, 没有坏处. - 省略
key
轻则性能损耗, 严重就会造成状态混乱的bug
单节点情况
和多节点原理一致, 如果省略了key且在内部使用了state, 二次更新时也有可能造成状态混乱
归根到底, 需要更新的组件省略key不可以, 静态的(永不更新的)组件就可以省略
from react-illustration-series.
Related Issues (20)
- 原理解析的一些误点? HOT 1
- fiber树构造阶段的问题
- fiber树构造(对比更新) -> 过程图解 -> performUnitOfWork第 4 次调用(执行beginWork和completeUnitOfWork)
- 移动端页面有问题,无法导航 HOT 2
- 网站好像挂了 HOT 2
- 网站中“运行核心”部分章节顺序错乱
- 网站的标题定位有点问题
- 链表操作的图是不是画错了 HOT 1
- hook-state 一节中 baseQueue拼接后的链表顺序好像有误
- 同学,18有计划吗 HOT 3
- 请问博客的生成框架用的是哪一个 HOT 2
- 请问下博主,是不是现在的16,17其实都是同步循环 HOT 3
- 啥时候更新React18呢
- fiber 优先级 HOT 1
- 图画错了 HOT 1
- commitBeforeMutationEffects 里处理的是哪些 Hook ? HOT 1
- mime模块拒绝服务漏洞 - [email protected] - 间接引入 HOT 2
- Can we port this to English in a fork HOT 6
- effect hooks图例的构造顺序是否有问题 HOT 2
- 反转链表代码有误 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-illustration-series.