Get a Reference to a DOM Element in React with the useRef Hook
Most of the time you don't need access to the actual DOM when developing React applications. But when you do need to, there's a hook that will give you access to the DOM.
In this lesson, we'll use the
useRef hook to get a reference to a
div element. Then we'll print the dimensions of that element - just to show that we have access to the actual DOM element; but you can use this element now to do anything you'd typically do with an element.
The one trick to using the
useRef hook is to remember to call
.current on the reference to get the actual element.