React Reference ยท react-dom Client APIs
React root.unmount() method
Reference for root.unmount(): remove a React tree from its container, run effect cleanups, and learn why a root cannot be rendered again afterwards.
root.unmount() removes everything React rendered into the root's container. Every component unmounts: effect cleanup functions run, refs are set to null, subscriptions you registered in effects are released, and the DOM nodes are removed. The container element itself stays in the page and is empty afterwards.
Normal apps never call it, because the tree lives as long as the page. It matters when React is embedded in something with its own lifecycle: a widget inside a non-React page, a modal opened by another library, a micro-frontend that mounts and unmounts, or a test that must clean up after itself.
const root = createRoot(container); root.render(<Widget />); // later root.unmount();
Try it
Syntax
root.unmount()
| Item | Description |
|---|---|
| Parameters | None. |
| Returns | undefined. |
| Errors | None for repeated calls: unmounting an already unmounted root does nothing. |
Usage notes
- Unmounting is final. After
unmount(),root.render()throwsCannot update an unmounted root.Create a new root withcreateRooton the same container if you need to render again. - Effects,
useLayoutEffectcleanups and ref callbacks (called withnull) all run during unmount. This is the right place to remove event listeners and close connections; if something leaks afterunmount, the component forgot its cleanup. - Unlike
root.render(null), which keeps the root alive and renders nothing,unmount()releases the root's internal data. Userender(null)when you may render again with the same root. - State inside the tree is thrown away. A new root starts fresh.
- The call is not guaranteed to finish before the next line of code. In tests, wrap it in
act()so cleanups are flushed before assertions. - Do not unmount from inside the tree itself during render. Trigger it from a handler or timer.
- A root from
hydrateRoothas the sameunmount(); afterwards the server-rendered HTML is gone too. - For portals and modals inside one root, do not use roots: render conditionally and let React remove them.