In React, the ref
attribute provides a way to directly interact with DOM elements or React components. While React encourages a declarative approach to UI updates, there are cases where imperative actions, such as focusing an input field, reading the value of an element, or interacting with third-party libraries, are necessary. ref
is the solution for such scenarios.
Key Concepts of ref
Refs are created using the React.createRef()
function or the useRef()
hook in functional components.
A ref is essentially a container that can hold a reference to a DOM element or a React component.
useRef()
hook is used to create refs.