Everything New in React 18

Pushkar Thakur
JavaScript in Plain English
5 min readJun 15, 2021

--

React 18 Alpha is here there are many things that got added in React 18. React 17 didn’t see many new additions. But that’s definitely not the case with React 18. In this article, we will look at some of the top updates.

What’s new?

1. The New Root API

Here’s what we’re used to seeing

We pass reactDOM.render our App component and then document.getElementById and our root element. So we’re rendering our app component into our root element on the page pretty simple.

Here’s the new way

We have a root variable that is equal to the new method called create root. This is being passed our root element and then we call root.render and pass our app component.

Our app component it’s accomplishing the same thing but in a different way. The old way called the legacy root API and it’s still going to work in react 18 but will be deprecated at some point by using the new root API you’re going to get access to all of the react 18 improvements including the concurrent features that we’re going to talk about in this article.

2. Suspense

In this update, we’re going to get full support for suspense. Now, what is suspense as the name implies it suspends something until it’s ready to be rendered.

In this example, we have a component that needs time to fetch data before it’s ready to be rendered suspense will use the fallback until the data is returned and the components are rendered.

It’s important to note here that the random component here is not waiting on data but it’s still going to be suspended until everything inside the suspense is ready to be rendered.

Now suspense is going to be extremely useful with server-side rendering currently with ssr you’re going to get fully rendered HTML but your browser still has to load the javascript and hydrate the entire page before it can become interactive suspense could speed up this load time dramatically using the example from the react 18.

Above we have a page loading a navbar, a sidebar, a post and a comment. Now we really don’t need the comments to load before the site becomes interactive so we’re going to suspend the comments so that the viewer can start reading the article and then we’ll load the comments in the background.

3. Automatic Batching

In React 17 and earlier react with batch re-render updates during a browser event like a click.

React 17 will not batch the re-renders if you need to fetch data and then update the state. With react 18 if you use the new create root API all state updates will automatically be batched no matter when they happen.

If there is some critical component you don’t want to batch. You can opt-out of this using ReactDOM.flushSync().

4. startTransition API

This is a new API introduced with this release, which helps in keeping the current webpage responsive and being able to do heavy non-blocking UI updates at the same time.

This will tell react which updates are urgent and which can wait this will help the UI remain responsive.

For urgent updates like typing, hover, clicking, we call props/functions usually like this :

For non-urgent or heavy UI updates, we can wrap it in a startTransition API as :

5. Suspense List

A <SuspenseList /> takes in revealOrder prop with values forward, backward, or together

Here the card component will be revealed in a forward direction(until the data is fetched, will fell back to LoadingSpinner Component). Similarly, backwards will reveal Cards in reverse order, and together prop will render everything "together".

6. useDeferredValue

useDeferredValue takes in the state value and a timeout in ms and Returns a deferred version of the value that may “lag behind” it for at most timeoutMs.

This is commonly used to keep the interface responsive when you have something that renders immediately based on user input and something that needs to wait for a data fetch.

We can install React 18 and reactDOM right away using:

Wrapping Up

React 18 is an alpha release right now and not suitable for production but it is always better to start learning about these features now.

React 18 will go into public beta in a few months.

Thank you for reading. Signing off. 🙌

Feel free to reach out. 👇

GitHub: https://github.com/Push9828
Twitter: https://twitter.com/PushkarThakur28
LinkedIn: www.linkedin.com/in/pushkarthakur28

More content at plainenglish.io

--

--

AWS Community Builder | Web Developer 👨‍💻 | Love to explore 🔍 new stuff