How to Upgrade to React 18
Key Points
- createRoot replaces ReactDOM.render
- Automatic batching across async boundaries
- Strict Mode simulates unmount/remount in dev
Summary
This guide summarizes the practical steps to upgrade apps to React 18: install the new package, migrate client and server rendering APIs, update TypeScript types, handle testing and Strict Mode changes, and take advantage of automatic batching and new hooks. Follow the checklist below to perform a safe, incremental upgrade.
Key Points
-
Install:
- npm:
npm install react react-dom - yarn:
yarn add react react-dom
- npm:
-
Migrate client APIs:
- Replace
ReactDOM.renderwithcreateRoot:const root = createRoot(container); root.render(<App />)
- Replace
unmountComponentAtNodewithroot.unmount(). - Replace server hydration
hydratewithhydrateRootfor SSR.
- Replace
-
Server rendering:
- Use
renderToPipeableStream(Node) andrenderToReadableStream(edge) for streaming with Suspense. renderToStringandrenderToStaticMarkuphave limited Suspense support;renderToNodeStreamis deprecated.
- Use
-
Automatic batching:
- With
createRoot, updates are batched across async boundaries (timeouts, promises, native events). - Opt out with
flushSyncfromreact-domwhen needed.
- With
-
TypeScript changes:
- Update
@types/reactand@types/react-domto latest versions. childrenoften must be declared explicitly in props; run the provided migration script and fix type errors.
- Update
-
New library hooks/APIs:
useSyncExternalStore(for external stores),useInsertionEffect(CSS-in-JS), and concurrent helpers likestartTransition,useDeferredValue,useId.
-
Strict Mode (dev-only):
- React 18's Strict Mode simulates unmount/mount to reveal effects that assume single mount. If you hit failures, temporarily remove or scope
StrictModewhile fixing issues before re-enabling it.
- React 18's Strict Mode simulates unmount/mount to reveal effects that assume single mount. If you hit failures, temporarily remove or scope
-
Testing:
- Set
globalThis.IS_REACT_ACT_ENVIRONMENT = truein your test setup to enable properact(...)behavior withcreateRoot.
- Set
-
Browser support:
- React 18 drops Internet Explorer support; plan polyfills or browser support updates accordingly.
Follow these steps incrementally: migrate roots, run tests, update types, then opt into concurrent features when ready.