In this tutorial, you will learn how to get access to the actual DOM in React using so-called refs.
To do that, we will take a look at what refs are and when and how to use them.
You will also learn about callback refs and what they are used for.
During this tutorial, we will take a look at different examples including refs in functions components and refs in hooks.
Let’s get started!
What is a ref in React?
When working with React, we are typically manipulating the DOM (Document Object Model) indirectly using the JSX syntax. A ref in React provides a way to directly access a particular DOM element.
This is quite useful when more control over the DOM element is required. Refs are commonly used to implement Animations or when using legacy libraries like JQuery that require direct DOM-access. Also focus, text-selection and media playback are great examples for valid use cases.
Refs also allow to getting reference to a child-component that is introduced using JSX declaratively.
When not to use refs
The main reason to use a frontend framework like React is to not have to deal with the DOM. Instead, we want React to handle all the DOM manipulation for us.
Using refs excessively is defeating that purpose. Manipulating the DOM directly should be considered a last resort.
Also, using refs to access components should be avoided. Often, it is possible to achieve the same thing in a declarative way by moving state to a parent component and passing that state down as a prop.
How to create refs using createRef()
Refs are created using the React.createRef() method. They are then attached to the corresponding JSX element and often saved to a property for later use.
The div can then be manipulated using the regular DOM-API using the reference. In this example, we change the color when the div is clicked by the user:
Using refs with components
When using ref to reference a component, we get access to the component instance itself instead of a DOM element. That means that we can call methods of that component.
In this example, the ExampleDilog component has a method called “open” that takes a message-string. We can call that method using the reference to the component:
More control with callback refs
Instead of creating a reference with React.createRef(), it is also possible to pass a callback function to the ref attribute. This function is called when the DOM element is created and receives the HTML DOM element. This element can then be stored and used later.
The element provided to the callback function is not a reference but the DOM element itself. That means that there is no “current” property.
Using refs inside functional components
Refs can be used in functional components, as well. It works quite similar to a regular component. The only difference is, that we don’t have instance properties, so we use regular variables to store the reference.
Functional components themselfs can’t be referenced using ref since they don’t have an actual instance.
How to use refs with hooks
Using the new hooks API the code is almost the same as the code of the functional component. The only difference is the usage of the useRef hook to create the reference.
Typing refs with TypeScript
In case you want to use TypeScript the type you will need is React.RefObject<> which is generic. The generic part specifies the type of the referenced element. This is typically some sub-type of HTMLElement or the type of a component.
Conclusion
In this tutorial, we learned how to use refs to manipulate DOM elements and components directly.
I hope you enjoyed this post.
If you did please share this post!
Thanks for reading!