Linguine Code

When to use React useRef and useLayoutEffect vs useEffect

In a previous article, I wrote about how to replicate a component lifecycle (componentDidMount, componentDidUpdate, and componentWillUnmount) with the React useEffect hook.

Somewhere in your googling you’ve also might have heard of another React hook called useLayoutEffect.

And some where in the readings and searching it might say that it acts like React useEffect but to only use it when doing DOM mutation.

But what exactly does that look like?

In this article I will go over that, and will actually use another hook called useRef which ties in together nicely!

When and how to React useLayoutEffect

The React useLayouEffect hook is written the same way as useEffect, and almost behaves the same way.

One of the key differences is that it gets executed right after a React component render lifecycle, and before useEffect gets triggered.

Let’s look at a piece of code.

We have a small application that displays a few div’s, a label, and a input field.

Before the return statement I’ve added a couple console messages to see the order of those messages.

useLayoutEffect and useEffect are only being executed after a component did mount lifeycle.

If we take a look at the result above, we can see that anything in the return statement gets executed first, aka the render cycle.

The next hook that gets executed is the useLayoutEffect hook, right after is the useEffect hook.

The real question is, why should we use this if it’s almost the same thing?

useLayoutEffect is identical to useEffect, but it’s major key difference is that it gets triggered synchronously after all DOM mutation.

You only want to use this hook when you need to do any DOM changes directly.

This hook is optimized, to allow the engineer to make changes to a DOM node directly before the browser has a chance to paint.

Now that you know when to use the useLayoutEffect hook, let’s look at another React hook that allows to access a DOM node the React way.

When and how to React to use useRef

In plain JavaScript you had to use getElementById or querySelector to select a DOM node.

But this is not an ideal solution in React.

In React you want to use the useRef hook or if you’re in a React class component, you want to use createRef.

The reason you don’t want to use getElementById or querySelector is because you may be designing your React app to output multiple of the same ID’s, which is a no no.

Another reason to use useRef is because it helps with the unidirectional (single direction) data flow.

You can define a node reference in a parent component and toss them down to child components.

Hence the single direction data flow.

Since our talk is about some of the React hook API, I’m going to use the useRef method.

Essentially, useRef is a hook function that gets assigned to a variable, inputRef, and then attached to an attribute called ref inside the HTML element you want to reference.

Pretty easy right?

React will than give you a an object with a property called current.

The value of current is an object that represents the DOM node you’ve selected to reference.

You should avoid using reference calls as much as possible. There are only 3 good reasons why you’d need to use the useRef hook.

  • Managing focus, text selection, or media playback
  • Triggering imperative animations
  • Integrating with third-party DOM libraries

Let’s create some code that can do something cool with hook functions that we’ve covered.

I’m goin to create an input field that has a label and a border that the width is 10% of the total width of the input field.

The label is going to mimic itself as a placeholder.

The goal is to make the label animate up and shrink, and make the border line to go 100% width when a user clicks on the input field.

This will require us to make changes to the DOM directly.

Using React useRef and React useLayoutEffect together

Now that you know when and how to use the hook functions, let’s put together the code challenge.

First I’ll create a CSS filed named App.module.css.

Pretty simple, and basic CSS code. The thing to keep an eye on is the .active class.

That is the class that is responsible for changing the position and the size of the label, and the width of the border of the input field.

Now I’ll create the React component next.

Very basic and easy steps.

The first step is to to create the DOM node references for the group div element and the input field element.

The div element with a class name of inputGroup is going to be the element that receives the .active class name.

Second step is to create the handler functions for the blur, and focus event.

In the focus event, a class named .active will be added to the inputGroupRef node reference.

In the blur event, the class named .active will be removed from the inputGroupRef node reference.

In step 3, I’m adding the event listeners and attaching the handler functions with it.

Step 4 is the clean up process. I’m removing all the event listeners from memory when the React component goes through the componentWillUnmount lifecycle.

In case you want to see the code go to this link. Github Source.

Conclusion

99% of the time you’re going to want to use the useEffect hook, and the useState hook to manipulate the output of a React component.

But in certain cases you need make changes directly to a DOM node.

In case you need to, follow these 2 rule of thumbs:

  • Use useRef if you need to manage focus, text selection, trigger imperative animations or integrating third-party libraries.
  • Use the useLayoutEffect when ever you need to use useRef.

Happy coding!