refs in ReactRefs offer a way to access DOM nodes or React elements created in the render method.
In a typical React data flow, props are the only way for a parent component to interact with a child component. To modify a child component, you need to re-render it with new props. However, in some cases, you may need to forcefully modify a child component outside of the typical data flow. The modified child component could be an instance of a React component or a DOM element, and React provides solutions for both of these scenarios.
Avoid using refs to perform anything that can be achieved declaratively. Typically, do not rely on refs when props and state can be used. However, there are several scenarios where using refs is appropriate:
DOM libraries.The ref property provided by React represents a reference to the actual instance of the component returned by ReactDOM.render(). It is important to differentiate between rendering a component and rendering a native DOM element. When rendering a component, the returned value is the component instance, while rendering a DOM element returns a specific DOM node. React provides three ways to use ref.
The ref can be directly set as a string value, but this approach is not recommended and may not be supported in future versions of React. This is mainly due to some issues caused by using strings. For example, when ref is defined as a string, React needs to track the currently rendering component. During the reconciliation phase, the process of creating and updating React Element, the ref will be encapsulated as a closure function and will be executed during the commit phase. This may have some impact on React performance.
React allows adding a special property to any component, and the ref property accepts a callback function that is executed immediately when the component is loaded or unloaded.
ref property to an HTML element, the ref callback receives the underlying DOM element as a parameter.ref property to a component, the ref callback receives the current component instance as a parameter.null is passed in.ref callback is executed before lifecycle callbacks such as componentDidMount or componentDidUpdate.We often use Callback Ref in the form of an inline function, which is re-created on every render. Since React cleans up the old ref and sets a new one, it will be called twice during the update, first with null. If there is business logic in the Callback, it may cause errors. This can be avoided by defining the Callback as a class member function and then binding it.
In React v16.3, the new React.createRef API was introduced through the 0017-new-create-ref proposal. When the ref is passed to an element in the render, a reference to that node can be accessed in the ref's current property. The value of the ref differs based on the type of node:
ref property is used for an HTML element, the ref created using React.createRef() in the constructor receives the underlying DOM element as its current property.ref property is used for a custom class component, the ref object receives the mounting instance of the component as its current property.ref property cannot be used on function components because they do not have instances.Comparing CreateRef with Callback Ref, there is no overwhelming advantage; it is just meant to be a convenient feature. There may be a slight performance advantage with CreateRef as Callback Ref adopts the pattern of allocating ref in the closure function during the component render process, while CreateRef adopts the Object Ref.