Open In App

How to upgrade to React 18

React 18 evolves the popular JavaScript component framework with new features built around concurrent rendering and suspense. It promises better performance, more capabilities, and an improved developer experience for apps that make the switch.

In this article, we’ll guide you through the steps to upgrade to React 18. Along with this, we will also showcase some of the features that are introduced in React 18.



Installing React 18

Step 1: To install the latest version, from your project folder run the following from the terminal:



For npm:

npm install react react-dom
               // OR
npm i react@latest react-dom@latest

For yarn:

yarn add react react-dom

Step 2: Update to Client Rendering APIs.

React 18 introduces a new root API which provides better ergonomics for managing roots. The new root API also enables the new concurrent renderer, which allows you to opt into the concurrent features.

Before:

import { render } from 'react-dom' ;
const container = document.getElementById('app') ;
render(<App tab="home" />, container) ;

After:

import { createRoot } from 'react-dom/client';
const container = document.getElementById('app');
const root = createRoot(container); // createRoot(container!) if you use TypeScript
root.render(<App tab="home" />);

Note: Your application will work without using the root API. If you continue to use ReactDom.render your application will behave like React 17.

Some updates of React 18

1. React changed unmountComponentAtNode to root.unmount :

Before:

unmountComponentAtNode(container);

After:

root.unmount();

2. React removed the callback from render:

Before:

const container = document.getElementById('app');
render(<App tab="home" />, container, () => {
  console.log('rendered');
});

After:

function AppWithCallbackAfterRender() {
  useEffect(() => {
    console.log('rendered');
  });
  return <App tab="home" />
}
const container = document.getElementById('app');
const root = createRoot(container);
root.render(<AppWithCallbackAfterRender />);

Note: There is no one-to-one replacement for the old render callback API — it depends on your use case.

3. Upgraded hydrate to hydrateRoot :

Before:

import { hydrate } from 'react-dom';
const container = document.getElementById('app');
hydrate(<App tab="home" />, container);

After:

import { hydrateRoot } from 'react-dom/client';
const container = document.getElementById('app');
const root = hydrateRoot(container, <App tab="home" />);
// Unlike with createRoot, you don't need a separate root.render() call here.

4. Dropping support for IE:-

React 18’s new features are developed with contemporary browser capabilities like microtasks, which Internet Explorer is unable to polyfill well enough. React 18 officially drops support for Internet Explorer (IE) due to its low usage and security vulnerabilities. This means that IE users will no longer receive official compatibility updates or bug fixes for React applications.

Impact: If your application targets IE users, you’ll need to consider alternative strategies, such as:

5. Updates to Server Rendering APIs

React 18 introduces new server rendering APIs to enable streaming capabilities and improve performance:

Impact: If you’re using server-side rendering (SSR) in your React application:

6. Updates to Client Rendering APIs

The primary change in client rendering involves the new createRoot API:

Impact: Update your client-side code to use createRoot instead of ReactDOM.render.

The basic structure remains similar:




// After React 18
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(<App />);
 
// Before React 18
ReactDOM.render(<App />, document.getElementById('root'));

7. Deprecations

React 18 deprecates a few APIs or behaviors that are considered outdated or less optimal:

Impact: If you’re using any of these deprecated APIs, update your code to use the recommended alternatives to ensure compatibility and maintainability.

Major Changes in React 18:

Beyond the specific API updates and deprecations, React 18 introduces some significant architectural changes:

Impact: Thoroughly test your application after upgrading to React 18, paying close attention to any potential behavior changes related to concurrent rendering and automatic batching. Consider using the React DevTools profiling features to identify any performance bottlenecks or unexpected behavior.

Changes to React DOM Server in React 18:

React 18 separates the React DOM library into two parts: react-dom for client-side rendering and react-dom/server for server-side rendering.

Impact: If you’re using server-side rendering, import react-dom/server explicitly where needed in your server-side code.


Article Tags :