Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React: 循环时加key的作用 #53

Open
leslie1943 opened this issue Nov 23, 2020 · 0 comments
Open

React: 循环时加key的作用 #53

leslie1943 opened this issue Nov 23, 2020 · 0 comments

Comments

@leslie1943
Copy link
Owner

leslie1943 commented Nov 23, 2020

React: 循环时加key的作用

  • Virtual DOM 在进行 diff算法 的时候, 根据key判断是否重新渲染, 在key值唯一的情况下组件没有修改的时候,复用此节点.
  • 由于 virtual dom 在比较的时候是同层对比, 如果没有Key, 即使是相同的节点也会删除重新创建, 如果有了key的加持,就可以复用之前的节点, 省略掉删除重建的过程
  • key的作用主要是为了高效的更新虚拟DOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant