On March 29, React 18 was officially released, and this version includes a number of out-of-the-box improvements such as automatic batching, new APIs such as startTransition, and streaming server-side rendering with Suspense support.
Many of the features in React 18 are described as being built on the new concurrent renderer. This is a behind-the-scenes change that unlocks powerful new features. Concurrent React is optional - it is only enabled when users use the concurrency feature - but the development team believes it will have a significant impact on the way the public builds applications.
The development team says its vision for Suspense has always been more than just loading code - the goal is to extend support for Suspense so that eventually the same declarative Suspense fallback can handle any asynchronous operation (loading code, data, images, etc.).
And development of the server component, which is still in beta, allows developers to build applications across servers and clients, combining the rich interactivity of client applications with the improved performance of traditional server rendering. This feature is expected to be released in an initial version in the 18.x minor release.
React 18 New Features
Automatic Batch Processing
Batching is React’s way of grouping multiple state updates into a single re-render for better performance. Without automatic batching, we would only be able to batch updates in the React event handlers.
By default, updates inside Promise, setTimeout, native event handlers, or any other event are not batched in React. With auto-batching, these updates will be automatically batched.
Transitions is a new concept in React that distinguishes between urgent and non-urgent updates.
- urgent updates reflect direct interactions, such as typing, clicking, pressing, etc.
- Transition updates transform the UI from one view to another.
New Suspense functionality
Suspense allows you to declaratively specify the loading state of a part of the component tree if it is not ready to be displayed.
Suspense makes the “UI loading state” a first-class declarative concept in the React programming model. This allows us to build higher-level functionality on top of it.
In React 18, the server adds support for Suspense and extends its capabilities with concurrent rendering features, and Suspense in React 18 works best when used in conjunction with the transition API. If you suspend during a transition, React will prevent content that is already visible from being replaced by a fallback.
Instead, React will delay rendering until enough data has been loaded to prevent incorrect loading states.
New Client and Server Rendering APIs
In this release, the development team has redesigned the APIs they expose for rendering on the client and server. these changes allow users to continue using the old APIs in React 17 mode when upgrading to the new APIs in React 18.
New Strict Mode Behavior
This feature will provide better out-of-the-box performance for React applications, but requires components to be resilient to effects that are mounted and destroyed multiple times. Most effects work without any changes, but some effects assume they are only mounted or destroyed once. To help address these issues, React 18 introduces a new dev-only check for strict mode.
This new check will automatically uninstall and reinstall each component whenever it is installed for the first time, and restore the previous state on the second installation.