Blog

Home / Blog

The new version of React 18 comes with Circumstantial Rendering, Automated Batching

Billy Yann
Data Scientist
Deep learning and machine learning specialist, well-versed with experience in Cloud infrastructure, Block-chain technologies, and Big Data solutions.
April 12, 2022


The new version of the react community, React 18, has finally been released after much anticipation and speculation about the major features this version will offer to developers and clients alike. Throughout the React development cycle, the React team was careful about releasing these innovative updates at a pace that React developers would fully understand and adopt. To join the React 18 working group, they invited a panel of experts, library authors, developers, and educators from various fields. And these experts were invited to contribute ideas, provide feedback, and collaborate on the release. Its goal is to prepare libraries and applications for easy and seamless adoption of React 18.

React 18 introduces many new features and changes that will redefine the framework over the coming years. React 18, a significant progression to the well-known JavaScript library is now available as an assembly update, highlighted by a new circumstantial renderer and automated update batching. The upgrade to React 18 has been carefully considered and measures are taken to ensure that your existing code will not be affected. Discover React 18's latest features and improvements that will revolutionize how the world reacts!

Concurrent Mode of React 18

One of the most impressive React 18 new features is the concurrency feature that solves an age-old problem that had been ignored. In reality, it is not exactly a feature, but rather a function that powers React to simultaneously prepare multiple instances of the UI. It's this adequacy behind the scenes that serve as the foundation for Acknowledge 18's multitude of new features. The React API hides implementation details from developers, and the API is designed that way. The React community believes that developers should focus on how using React features will enhance the user experience they want to provide for their clients, and React will figure out how to deliver it. 

The React community believes that React developers should concentrate on how using React functionalities will aid them to reach the required customer experience for their customers. And React will start figuring out how to provide that user experience. However, Concurrent React isn't just a technical detail. React 18 has a fundamental React change that updates the core rendering model, explaining how concurrent React performs. 

In earlier versions of React 18 or even before advancement to React 18 and disabling concurrent features, updates were performed in a single, uninterrupted, and synchronous transaction. Asynchronous transactions are problematic in that once you begin to render an update; you cannot interrupt it until the result is displayed on the user's screen.

Concurrent rendering does not always result in this outcome. Depending on what the situation is, React can initiate rendering an update, pause it in the middle, or cancel an already in progress rendering update. With React, users will always have a seamless user experience and consistent UI regardless of whether a render is interrupted. This allows React to develop additional screens behind the scenes without interfering with the main thread. Hence, the user's input will be processed immediately by the main thread, regardless of whether it is stuck rendering middle to large updates. It creates a smooth user experience.

A reusable state is another advantage of concurrent React. Concurrency enables React to remove portions of a user's screen whenever they are no longer needed and add them back whenever necessary by reusing their previous state. By switching tabs and returning to the original tab when a user returns, React will be able to retain the state of the original tab. A new component called *offscreen> is due to be introduced in React 18 to implement this pattern. 

Automatic Batching

The event handlers and inbuilt hooks in React have always been used to group state updates. It prevents all state updates from necessitating unnecessarily re-rendering components, which results in improved performance. Just internet explorer events were endorsed for batching in React 17. The new React 18 release launches an improved batching experience called "Automatic Batching". It makes batching easier for all state new features, irrespective of where they come from with createRoot. 

In React 17 and previous renderings, updates were only batched after the event called a callback to update both functions, which resulted in the React application re-rendering twice. Despite this limitation, React 18 was able to handle promises, timeouts, native event handlers, and any additional events handled by React 17 via batch updates. This will substantially lower extraneous re-renders, resulting in improved application performance.

Using flushSync() to stop automatic batching

In React 17 and previous versions of the app, rendering was unnecessary and was reduced by automatic batching. A way should also be provided to prevent automatic batching for any code that relies on reading something from the DOM straightaway when a state change occurs. React 18 includes a feature called ReactDOM.flushSync() that allows developers to opt out of batching.

Transitions

Transition is an innovative React attribute that helps to distinguish between both urgent and non-urgent updates. This allows developers to prioritize updates they expect their users to interact with more, or wish to experience more smoothly.

In the case of pressing updates, straight interactions are reflected by tapping, typing, or pressing. The UI is transitioned from one view to another through these updates. When dealing with physical objects, it is important to respond immediately for the user to match their instincts and experience. The slightest delay when clicking a button throws them off since they expect instantaneous results. Users are more attentive to transition updates because they recognize that their activities, such as switching from one page to another, will not result in a quick reaction on the screen. Users are accustomed to slight delays when they click a drop-down menu, for example. This is a natural way of displaying content. Users will be patient with updates, especially when you filter or sort the data following new attributes. 

To give users complete satisfaction, every specific user input should contribute to both non-urgent and urgent changes. With React 18, development teams can use the begin transition API within an input event to notify React of what new features are pressing and which are just "transitions."

If an urgent update comes in from the user, such as a keypress or click, then the updates wrapped inside the start transition will be interrupted. When the user interrupts a transition, React abandons the mid-rendering task that isn't finished and prioritizes rendering the urgent update.

In React 18, there are two different ways to begin transitions. 

Use Transition: a hook for beginning, transitions, and a value for monitoring the awaiting state

Start transition: Development teams can use this technique to begin transitions when hooks are not accessible.

React 18 suspense features

React suspense is a React element that enables development teams to postpone an element while it is being performed and shows a fallback option. A string or another React component, such as a spinner, can be used as a fallback option. Up until now, Only dynamic importing would allow React suspense to function.

As a result, React suspense has been used to assist development teams in presenting a majestic loading state to users when there is an interruption where the connectivity has completed loading and is implementing the next chunk of JavaScript code. You could notify your users using React suspense that the web app is syncing the next chunk and that the information will be displayed to them shortly.

With React 18, development teams at React began to diversify the capabilities and effectiveness of React suspense, particularly by incorporating it with the previously mentioned concurrent rendering attributes of React 18. Development teams can now use React suspense in conjunction with the transition API. If a transition is paused, React hinders the currently viewable content from being replaced by a set of fallbacks. Rather than, React will now pause rendering until enough information has been loaded to avoid a bad loading condition.

Strict mode behaviors

A new mode called 'strict' has been introduced into React 16.3. The React encoding detection method helps detect issues with concurrent rendering APIs that may be caused by the encoding method. It goes in development mode and displays error messages or code recommendations to enable them to improve their code. By including *Strictmode> in the React app, the development teams include new behavior patterns to all of the apparatus that it encases. React, for example, automatically re-renders all components when run in strict mode, thereby preventing any potentially harmful effects.

React hooks

Last but not least, new React hooks have been added! In October of 2018, React hooks were made publicly available. They offer the development team a substitute for creating class-based components as well as a fresh angle on state management and lifecycle functional areas.

Conclusion

These are the most recent trends and innovations in React 18 that you should be aware of. Stay tuned to this space for more updates on React 18 and the latest what’s new in React as and when it becomes available.