Learn How to Use React 18 Hooks

In March 2022, the React team announced the official release of React 18. This release came with a host of new features geared at performance improvement, based on the concept of “concurrent rendering”. The idea behind concurrent rendering is to make the process of rendering to the DOM interruptible.

Among the new features are five hooks: useId, useTransition, useDerredValue, useSyncExternalStore, and useInsertionEffect.

4

React useTransition Hook

By default, all React state updates are urgent. Different state updates in your application compete for the same resources, slowing it down. The useTransitionReact hooksolves this problem by letting you mark some state updates as non-urgent. This allows urgent state updates to interrupt those with a lower priority.

The SearchPage Component

This simple program imitates a search engine that updates two states—an input field and some search results.

The Updated App Component

The code above renders a React application with an input field:

As you begin to type characters into the field, 30,000 copies of the typed text will appear below:

Open Source App Icons on a BENQ Minitor

If you type several characters in quick succession, you should spot a delay. It affects the time that characters take to appear in both the input field and the “search result area”. This is because React is running both state updates at the same time.

If the demo runs too slowly or too quickly for you, try tweaking thelistSizevalue accordingly.

Meta AI being used on a laptop and cellphone

Inserting the useTransition hook into the application will allow you to prioritize one state update over the other.

Using the useTransition Hook

Updating yourSearchPagecomponent with the code above will prioritize the input field over the “search result area”. This simple change has a clear effect: you should start to see the text that you type into the input field immediately. Only the “search result area” will still have a slight delay. This is due to thestartTransitionapplication programming interface (API)from the useTransition hook.

The code that renders the search results to the UI is now using thestartTransitionAPI. This allows the input field to interrupt the search results’ state update. When theisPending()function displays “…Loading result” it indicates that a transition (from one state to the next) is happening.

Person holding a phone showing the Tor browser logo

React useDeferredValue Hook

The useDeferredValue hook allows you to defer the re-rendering of a non-urged state update. Like the useTransition hook, the useDeferredValue hook is a concurrency hook. The useDeferredValue hook allows a state to keep its original value while it is in transition.

The SearchPage Component With the useDeferredValue() Hook

In the code above you will see that theisPending()function no longer exists. This is because thedeferredValuevariable from the useDeferredValue hook replaces theisPending()function during state transition. Instead of the search results list refreshing when you type a new character, it will keep its old values until the application updates the state.

React useSyncExternalStore Hook

Unlike the useTransition and useDeferredValue hooks that work with application code, useSyncExternalStore works with libraries. It allows your React application to subscribe to and read data from external libraries. The useSyncExternalStore hook uses the following declaration:

This signature contains the following:

React logo

With the useSyncExternalStore, you can subscribe to an entire data store or a specific field within a data store.

React useInsertionEffect Hook

The useInsertionEffect hook is another new React hook that works with libraries. However, instead of data stores, the useInsertionEffect hook works with CSS-in-JS libraries. This hook addresses style rendering performance issues. It styles the DOM before reading the layout in the useLayoutEffect hook.

React useId Hook

You use the useId hook in situations that require unique IDs (except keys in a list). Its main purpose is to generate IDs that remain unique across client and server, avoiding the React server hydration mismatch error. The useId hook uses the following declaration:

In the declarationidis a unique string that includes the: token. After declaration, you can pass theidvariable directly to the element(s) that need it.

What Value Does These New Hooks Add to React?

The useTransition and useDeferredValue hooks are application code hooks. Through concurrency rendering, they improve the performance of applications. The useId hook tackles the hydration mismatch error by creating unique IDs across client and server.

The useSyncExternalStore and useInsertionEffect hooks work with external libraries to facilitate concurrency rendering. The useInsertionEffect hook works with CSS-in-JS libraries. The useSyncExternalStore hook works with data store libraries like Redux store.

Together these hooks give a major boost to performance, which in turn improves user experience.

Manage React state with ease using Redux. Ready to get started? Here’s how.

Make sure you don’t miss these movies and shows before Netflix removes them.

Windows is great, but adding this makes it unstoppable.

My foolproof plan is to use Windows 10 until 2030, with the latest security updates.

Turn these settings on, and your iPhone will be so much better than before.

I gripped my chair the entire time—and then kept thinking about it when the screen turned off.

Technology Explained

PC & Mobile