I'm not quite sure there's a real answer to this question but I was wondering if it's better to find regular DOM elements in a React app by using
a. refs and ReactDOM.findDOMNode
or
b. plain old document.getElementById
I would prefer the pattern of refs because it would encourage you not to add IDs to elements and therefore allow you to use multiple instances of a react component on a single page. The ref feature in react will get you the element rendered by that specific instance of the component, and not an element from any duplicate renderings of it.
I don't really know what you're asking. If you're asking in terms of speed, getElementById() is O(1) in modern browsers. However, using DOM methods like that have the disadvantage of piercing the abstraction React provides.
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://stackoverflow.com/questions/36415198/finddomnode-vs-getelementbyid-for-plain-dom-elements