Giter VIP home page Giter VIP logo

Comments (2)

thomas-void0 avatar thomas-void0 commented on May 18, 2024

元素key属性的作用是用于判断元素是新创建的还是被移动的元素,从而减少不必要的元素渲染。

from react-illustration-series.

7kms avatar 7kms commented on May 18, 2024

重新整理了一下哈, key的作用那篇写的比较草, 现在优化了一下.

  1. key的作用就是服务于diff算法, 是节点是否可以复用的首要判定条件.
  2. 如果省略了key, 内部会默认使用null, 在列表节点有排序需求的情况下, 会造成性能损耗.

在多节点的情况下

除非是静态列表渲染, 没有更新动作, 可以省略key.

一般在列表需要二次渲染更新的情况下:

  • 加上key只有好处, 没有坏处.
  • 省略key轻则性能损耗, 严重就会造成状态混乱的bug

单节点情况

和多节点原理一致, 如果省略了key且在内部使用了state, 二次更新时也有可能造成状态混乱

归根到底, 需要更新的组件省略key不可以, 静态的(永不更新的)组件就可以省略

from react-illustration-series.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.