React Reference Guide: Refs and the DOM
Blog post from LogRocket
In React, refs are a powerful tool for accessing DOM nodes or React elements created in a component's render method, offering a way to perform imperative actions like managing focus, text selection, or integrating with third-party libraries. While props are typically used for component interaction, refs are essential when you need direct access to a DOM element, which is not possible through props alone. React provides two main ways to create refs: using `React.createRef` or callback refs, with the former being introduced in React 16.3. Refs can be added to DOM elements and class components, but they should be used sparingly as they bypass the declarative nature of React's data flow. Function components cannot directly receive refs unless using `forwardRef` or similar approaches. Callback refs offer more control over when refs are set or unset, but care should be taken to avoid unnecessary re-renders. While once common, string refs are now deprecated and should be avoided.