React useRef Hook

The useRef Hook in React allows you to maintain values across renders without causing a re-render when the value changes.

Tutorials dojo strip

It can be used to store a mutable value and directly access DOM elements.

Avoiding Re-renders

Using the useState Hook to count application renders can lead to an infinite loop as useState causes a re-render. Instead, the useRef Hook can be used to avoid this.

Example

Using useRef to track application renders:

JavaScript

The useRef Hook returns an object called current. When initializing useRef, you set the initial value: useRef(0). It’s similar to: const renderCount = { current: 0 }. Access the count via renderCount.current.

Try running this code and typing in the input field to see the application render count increase.

Accessing DOM Elements

React typically handles all DOM manipulation. However, useRef can be used in specific cases without causing issues.

Add a ref attribute to an element to access it directly in the DOM.

Example

Using useRef to focus an input:

JavaScript

Tracking State Changes

The useRef Hook can also keep track of previous state values since it persists values between renders.

Example

Using useRef to track previous state values:

JavaScript
Tutorials dojo strip