【问题标题】:Which is more performant at scale, `className` or `ref`?`className` 或 `ref` 哪个在规模上性能更高?
【发布时间】:2020-02-19 05:55:58
【问题描述】:

屏幕上有很多元素,所以我需要很多唯一标识符。我可以使用className="uniqueItem"ref={ref => this.uniqueItem = ref} 来做到这一点,那么当有很多组件经常重新渲染时,哪个性能更高?

【问题讨论】:

    标签: reactjs performance ref


    【解决方案1】:

    Ref 会很好,因为它还可以为您提供很高的可重用性,在 className 的情况下会更少。

    但是说到性能,className 会更好。

    【讨论】:

    • 可能是常识,但任何支持该声明的 jsperf 链接或 url 引用?
    • 我质疑的原因是因为ref 标记的DOM 元素似乎在React 生命周期中比className 标记的元素更早可用,所以这让我猜测ref 会更多高性能。
    猜你喜欢
    • 2010-10-15
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2011-11-14
    • 2012-10-08
    • 2014-02-05
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多