Get a Reference to a DOM Element in React with the useRef Hook

Share this video with your friends

Send Tweet

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.

Konekoya
Konekoya
~ 5 years ago

Love this playlist. Would love to see more videos like this :) Please keep up the good work!

Chris Achard
Chris Achard(instructor)
~ 5 years ago

Thanks @Koneykoya! I actually do have a couple more hooks videos planned :) Plus more playlists to come!