The Complete Developer Guide to React 19, Part 1: Async Handling

Hristo Totov
No items found.

In short

React 19 beta introduces features aimed at enhancing state management, error handling, and async operations. While primarily for feedback from library developers, many features are stable. This first part of the guide focuses on new hooks like useTransition and useActionState, which streamline async handling, improve form management, and support optimistic updates for a better user experience.

Introduction

React 19 beta has been available on npm for some time now. While this release is currently intended for library developers and maintainers to gather feedback and improve the future stable version, most of its features can be considered stable.

These newly added features aim to assist application developers in their day-to-day tasks, which include state, error, and asynchronous handling. If used correctly, these updates should improve workflows, make applications faster, and simplify development.

This guide aims to review the most notable features, explain when they could be helpful, and explain how to use them with practical examples and short explanations. It comes in three parts—this is just the first part, focusing on Async Handling with Hooks. Part two delves into React 19 improvements and additions, while the last part explores upgraded resource support and presents overall conclusions:

Enhanced Async Handling with Hooks

Actions

Actions are intended to handle asynchronous requests and automate managing pending states, errors, and optimistic updates. Developers can handle async operations more intuitively now while keeping the UI responsive at all times - even when requests are happening live. 

Monitoring state with variables like isPending or isLoading during async operations can now be replaced with the new useTransition hook. useTransition allows you to update the component state without blocking the user interface. In other words, it makes the application run and feel smoother while performing async operations in the background, like a network request. One example of how to use the useTransition hook would be:

What's important to notice here:

  1. startTransition is content meaning that whatever is wrapped inside will not trigger a state update until all of the code inside is executed. In other words, no matter how many state updates are performed inside startTransition (in the example above only one - setError) all of them will come through as one update at the end. This makes the hook extremely useful as it would only cause one UI rerender instead of N rerenders where N is the number of state changes performed inside handleSubmit.
  2. Previously we would have to use an isPending state variable to detect whether updateName has been completed and the code for that would look something like this:
Notice that there might be a point where pending is set to false, but we don’t have the error just yet.

Now imagine if you had multiple async operations between setPending(false) and if(error) - as you can imagine it would not be a very nice user experience. This cannot happen in startTransition because it would produce only one state update at the end - after everything is completed. Moreover, it saves us one line of code!

  1. isPending will automatically be set to false after any transition which allows the developer to efficiently update the UI while the action is going and not have to worry about handling loading logic while the data is changing.
  2. Functions that use async transitions are called “Actions” by convention. Actions provide a pending state which automatically resets, supports optimistic updates, provides error handling to allow for Error Boundaries when a request fails and can also be passed to formform action props.

Better handling form

React 19 also introduces a new hook called useActionState which provides developers with built-in support for handling form states and submission. This hook allows for the automatic management of pending states and results. 

Here are a few important things to notice:

  1. Everything inside useActionState is action code i.e. the code to be executed when the submit happens
  2. formData is not really type-safe, meaning it’s not guaranteed that formData.get(name) will return text. A custom hook that uses useActionState with a validator could come in handy in order to validate data before passing it to API calls.
  3. submitAction is directly passed in the <form /> element action prop. useActionState is specifically for actions and especially for forms
  4. useActionState is specifically for actions and especially for forms.

In addition to useActionState, a new hook called useFormStatus has also been introduced to further simplify the usage of form elements in React 19. It allows developers to access form information in child components without having to drill down props or use context. The usage is straightforward and intuitive.

useFormStatus provides access to four form properties: pending, data, method, and action

An important thing to note here is that only child components of the form element would be able to access these properties i.e. DesignButton is a child component of <form /> in this example.

Optimistic updates

Optimistic updates are a way of instantly showing the user the final state of the UI optimistically even though data mutations are ongoing in the background. The application feels more responsive this way and it’s especially helpful in cases of bad connection or services that take a long time to resolve. In case of an error, the UI is reverted to the original state.

In React 19 this functionality can be implemented using the new useOptimistic hook.

What's important here: 

  1. useOptimistic takes an initial value just like useState.
  2. React will automatically revert optimisticName back to currentName when the submit finishes or errors.

New API use

React 19 introduces a way to read resources like promises and context in render using use. What is more, this new API can be used in conditionals as well which was previously uncharted territory for hooks. This change allows developers to manage data dependencies in a more effective way and focus on creating more dynamic and well-designed applications.

Reading a promise with use can be coupled with Suspense until the promise resolves, which is handy for network calls, for example:

An important thing to note here is that use does not support promises created in render. If you want to pass a promise, it must be cached or use a Suspense-compatible library or framework. use can also be used for conditional rendering and to read from Context, allowing for useful theme switching. Note that it cannot be called in a try-catch block, though.

An important thing to notice here is that there’s no need for context consumers and developers can just use use directly and obtain the context value even in conditional statements and early returns. Moreover, similar to useContext, use searches for the closest context provider that is above the component that calls it. This means that use does not consider context providers in the component from which it was called.

Server Components

React 19 introduces a way of rendering components ahead of time, either once at build time on a CI server or per each request using a web server. This happens in an environment separate from your client application called the “server”. If you have used Next.js before you might be familiar with the setup. 

With Server Components, React effectively allows developers to decide when the HTML file gets created—on a server, at build time, or at runtime, giving more flexibility and room for optimization.

Previously, a common way to read static data from a CMS, for example, would be to use an effect: 

With server components, this can be loaded at runtime, saving the client from having to download and parse 75K of libraries and wait for a second request to fetch the data after the page loads, just to render static content that will not change for the lifetime of the page.

Server Actions

This is a new functionality that allows client components to call async functions executed on the server. In other words, it is a binding between the server and the client. Defined with the ‘use server’ directive, these so-called Server Actions enable frameworks to automatically create a reference to the server function and pass that reference to the Client Component. 

Server Actions can be created in Server Components and passed as props to Client Components, or they can be imported and used in Client Components.

An important thing to note here is that ‘use server’ is not a directive you would put on top of a Server Component. Instead, it just creates a link between the server and the client. For example, putting ‘use client’ on top of a file informs React to send the JavaScript to the client where it can be used i.e. a tunnel from the server to send data to the client.

With ‘use server,’ it is the same but with the opposite direction - a tunnel on the server for the client to send data to. For example, to submit data with a form ‘use server’ would be used to let the server accept the form data from the client.

An example of a Server Component to add entries to a db would be:

When React renders the EmptyNote Server Component, it will create a reference to the createNoteAction function, and pass that reference to the Button Client Component. When the button is clicked, React will send a request to the server to execute the createNoteAction function with the reference provided:

In this example, the onClick Server Action is passed as a prop, but it could also be imported from a file:

Although these functionalities are different on their own, having access to both could be very handy for developers as it would improve reusability and modularity.

Other benefits of this new addition are improved SEO—more accessible content and enhanced performance—quicker initial page loads, especially for bigger applications that require a ton of resources and substantial content loads.

Now, let’s dive into the second part of the guide, where we’ll explore React 19’s improvements and additions, and the third part, where we talk about the upgraded support in React 19.

Latest update:
September 2, 2024

FAQ

No items found.
React Galaxy City
Get our newsletter

By subscribing to the newsletter, you give us consent to use your email address to deliver curated content. We will process your email address until you unsubscribe or otherwise object to the processing of your personal data for marketing purposes. You can unsubscribe or exercise other privacy rights at any time. For details, visit our Privacy Policy.

Callstack astronaut
Download our ebook

I agree to receive electronic communications By checking any of the boxes, you give us consent to use your email address for our direct marketing purposes, including the latest tech & biz updates. We will process your email address and names (if you have entered them into the above form) until you withdraw your consent to the processing of your names, or unsubscribe, or otherwise object to the processing of your personal data for marketing purposes. You can unsubscribe or exercise other privacy rights at any time. For details, visit our Privacy Policy.

By pressing the “Download” button, you give us consent to use your email address to send you a copy of the Ultimate Guide to React Native Optimization.