DEV Community

React useRef Guides

useRef persists values between renders.

This is a collection of top and trending guides written by the community on subjects related to React useRef concepts. For all things React, check out the React tag! Please contribute more posts like this to help your fellow developer in need.

Using refs in React functional components (part 1) - useRef + callback ref

We simply set the ref attribute in the <input> with a function instead of ref attribute created by useRef(). This function receives the DOM node and assigns it to the inputRef we declared before. Since we didn't create a ref with useRef the inputRef variable stores the DOM element itself then we don't need to access the .current property, as you can see in the onClick and onFocusClick functions.


Introduction to useRef Hook

This post is going to talk about what is useRef hook and when we can use it.


What is useRef hook and how do you use it?

A hook is a special function which enables one use state and other React features without writing ES6 class components which are generally considered difficult to understand, use and master.

useRef hook is part of the React Hooks API. It is a function which takes a maximum of one argument and returns an Object. The returned object has a property called current whose value is the argument passed to useRef. If you invoke it without an argument, the returned object's current property is set to undefined. The code below illustrates how to invoke the useRef hook in functional components.


Mutable and immutable useRef semantics with React & TypeScript

In this post, you will learn how different ways declaring a ref with useRef hook influence the immutability of the current ref property. We will be looking at how to make the current property immutable, mutable, and know without much effort if the ref is one or the other.


Understanding the use of useRef hook & forwardRef in React

The useRef hook in react is used to create a reference to an HTML element. Most widely used scenario is when we have form elements and we need to reference these form elements to either print their value or focus these elements etc.


¿Cómo rayos funciona el hook useRef en React? 🧐

Ahora bien, useRef accede a elementos del DOM de manera directa, sin aprovechar las bondades que nos da react, entonces, ¿por que quisiéramos hacer algo así? ¿No tiene mucho sentido verdad? Por ahora quédate con este pequeño concepto, con los ejemplos podrás comprender mejor.


Storing values with the useRef hook

React's useRef hook is used to store references to DOM elements. But did you know you can store and update values with useRef?


React forms and useRef Hook

So, in the last article we got to know about useState and useEffect hooks of react. Let's dive a little deeper this time with learning what are Forms and does useRef hook really work. I have also thrown in a bonus of what is prop drilling.


Don't be too dependent on useState, useRef is there for you

In the second example actually we don't need state. useRef is fine. Check the below code 👇🏻


Easily Detect Outside Click Using useRef Hook in React

We can use useRef for this.


How to useRef to fix React performance issues

But alongside the concept of Hooks, the React team introduced the useRef Hook, which extends this functionality:


React Hooks: useRef Explained in 2 Minutes

When building a React app, there are going to be times where you'll want to reference a particular part of the DOM for one reason or another. If you've written Javascript code before, you're likely very familiar with how you would go about doing this, however In React you'll have to change your ways a bit — luckily it is pretty painless — in comes the useRef hook.


Understanding useRef in react

In my previous post, we talked about useMemo, when we should use it and why we shouldn't overuse it. In this post, we will be looking at the useRef hook and the common use-cases. Let get into it right away.


Demystifying useRef and useMemo in React

UseRef is short for reference and is a hook which allows persisting data across renders, but does so without causing the component itself to rerender. It is used by invoking useRef function and passing an initial value to it. First let's look at the syntax and how to use the hook.


React's useEffect and useRef Explained for Mortals

This was helpful, because it provided a solution in the form of useRef (Thanks Dan!), but it left me in the dark as to how it would help avoid the issue (mostly because I didn’t understand useRef).


Understanding useEffect, useRef and Custom Hooks

In this article, we will learn at what time React invokes useEffect, useRef and custom hooks. A usePrevious hook will be used for demonstration.


Project 61 of 100 - React useRef Hook

Like other React hooks, to begin using the useRef hook we must first instantiate it in the component. This means, commonly, setting it equal to null because assignment happens on the component itself with the ref keyword. It is then stored in a variable for use later.


Need help with useRef in React !

Can someone explain me how to use UseRef in React? What it is and what it does? I have tried to find tutorial online but I still can't understand that stuff. Thank you so much !


Using React useRef Hook to access immediate past props or state.

Do you want to know what the previous state of a value is before it got updated in React?

You can easily leverage useRef to track the previous value of state or props in React.


React useRef Hook

As we already know that hooks in react is nothing but a function so , The useRef Hook is also a function which return an mutable object whose .current property is initialized with passed argument (initialValue).The returned object will persist for lifetime of the component.


Happy React useRef coding!