findDOMNode 与普通 DOM 元素的 getElementById

findDOMNode vs getElementById for plain DOM elements

我不太确定这个问题是否有真正的答案,但我想知道通过使用

在 React 应用程序中找到常规 DOM 元素是否更好

一个。参考文献和 ReactDOM.findDOMNode

b。普通老 document.getElementById

我更喜欢 refs 模式,因为它会鼓励您不要向元素添加 ID,因此允许您在单个页面上使用 React 组件的多个实例。 React 中的 ref 功能将为您提供由该组件的特定实例呈现的元素,而不是来自它的任何重复呈现的元素。

我真的不知道你在问什么。如果你问的是速度,getElementById() is O(1) in modern browsers。然而,使用 DOM 这样的方法具有穿透 React 提供的抽象的缺点。

You created multiple objects with the same ID. When any of the components asks for the element with ID myInput, the browser hands back the first one

https://www.andrewhfarmer.com/use-refs-not-ids/