Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Use the React useRef Hook to Reference a DOM Element or Simulate an Instance Variable

    Elijah ManorElijah Manor

    Refs have been a handy way to reference DOM elements in Class Components, but did you know you can use refs for other things as well? In this lesson, we’ll examine how to leverage the useRef hook inside a Function Component. We’ll first use this ref to attach to a React element, which you may already be familiar with, but then we’ll examine how to use a ref to simulate a Class instance variable.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: We'll start a dev server to run a simple react app that you can see on the right. The app is a react class component and uses a react.create ref so that the focus button can access the input dom element, set focus, and select the text.

    Let's take a look at the code for this and show how you could do the same thing with the react function component and hooks.

    The playground component has the code for the app we just saw. You can see the create ref in our constructor. We assign that to the ref prop of our input component. On the on click of the button, it calls the focus and slight methods off the dom element.

    In order to convert this class to a function component, we'll need to import use ref from react and then replace class with function. Next, we could remove the constructor and we'll introduce a local input ref variable and set it to the return of the use ref hook. Then, we'll convert the handle click method to a local function and remove the this.references in our file with nothing.

    Finally, we'll remove the render method and just return what we want to be rendered. It should work just as it did before, and it does. Nice.

    Refs can actually be used in another way. They weren't only made for dom elements. You can actually use refs to simulate instance variables like you would use in a class. Let's switch up our app to use a todo list, instead. Let's explore this alternate technique.

    The class version of our todo list app keeps track of the index values of each todo and increments them. If you refresh and create a new one, it's smart enough to take the highest value from before and increment that one.

    It starts at zero and when it reads from local storage, it updates to the maximum number that it finds. Then, on additions, it just increments from there.

    If we switch to the function component version of this todo list, we'll find that it takes the easy way out and just uses, which is the number of milliseconds since January 1st of 1970. If we run the app and add an item, you'll see our index is huge. Let's go and use a ref hook inside our function component to simulate what the class component was doing.

    First, we'll import the use ref hook from react. Then, we'll create a todo ID variable and assign it to the return of use ref. We'll initialize the ref to a value of zero.

    Let's go grab the logic from the class version to use in our new function component.

    Inside our initial todo's function, we'll expand our one liner function, save off our parsed storage and a value from storage variable, and make sure we return that from our function. However, right before, we'll paste the code from our class component, delete the this.part, make sure we access the current property off the ref, and replace todos with value from storage.

    That'll reduce over all the entries, looking for the maximum ID value.

    The last step is to increment our todo ID when creating a new one and to use that value instead of Now, we should be able to test our app again, but this time our ID should be the next in line. It is. It's five.