![]() |
VOOZH | about |
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.
Table of Content
Step 1: Create the React Project using CRA command
npx create-react-app myappStep 2: Move to the project directory
cd myappStep 3: Install React 18 as a dependency with the following command
npm i react@18 react-dom@18The updated dependencies in package.json file:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Step 1: To install the latest version, from your project folder run the following from the terminal:
For npm:
npm i react@18 react-dom@18For yarn:
yarn add react@18 react-dom@18Step 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 your application will behave like React 17.
Before:
unmountComponentAtNode(container);After:
root.unmount();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 />);
: There is no one-to-one replacement for the old render callback API — it depends on your use case.
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.
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:
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:
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:
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.
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.
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.