If you need to mount a React widget inside one specific div on a page, pass that div to createRoot() from react-dom/client and call root.render() with your component.
Older guides still use ReactDOM.render, which React 19 removed.
In this tutorial, I’ll show you how to mount a React component to a div three ways on one product page, plus the two errors you’ll likely hit.
The Sample Product Page We’ll Mount Components Into
Our sample is a product page for Lakeside Outdoor Co., a made-up Boise, Idaho store. React only owns the empty divs:
<header>Lakeside Outdoor Co. | Boise, ID</header>
<h1>Trailblazer 2-Person Tent</h1>
<div id="cart-widget">Loading cart...</div>
<div class="rating" data-label="Tent" data-stars="4.6"></div>
<div class="rating" data-label="Rainfly" data-stars="4.2"></div>
<script type="module" src="/src/main.tsx"></script>
The components live in src/App.tsx (typed props keep them honest):
import { useEffect, useState } from "react";
export function CartButton({ price }: { price: number }) {
const [qty, setQty] = useState(0);
return (
<button id="add" onClick={() => setQty(qty + 1)}>
Add to cart: ${price.toFixed(2)} ({qty} in cart)
</button>
);
}
type RatingProps = { label: string; stars: number };
export function Rating({ label, stars }: RatingProps) {
const full = "★".repeat(Math.round(stars));
return <p className="stars">{label}: {full} {stars} / 5</p>;
}
export function SaleBanner({ onClose }: { onClose: () => void }) {
useEffect(() => () => console.log("SaleBanner cleanup ran"), []);
return (
<div className="banner">
Black Friday: Trailblazer tent $199.99 through Nov 30, 2026
<button id="close" onClick={onClose}>Close</button>
</div>
);
}
I ran everything on React 19.3, react-dom 19.3, Vite 8.3 and TypeScript 7.0.2; here’s how to set up a React app with Vite.
Method 1: Mount a Component to One Div with createRoot (Recommended)
Use this for a whole app in #root or one widget on an existing page. Here’s src/main.tsx for Lakeside’s cart div:
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { CartButton } from "./App";
const el = document.getElementById("cart-widget");
if (!el) throw new Error("No #cart-widget div on this page");
const root = createRoot(el);
root.render(
<StrictMode>
<CartButton price={249.99} />
</StrictMode>,
);
console.log("right after render():", el.textContent);
setTimeout(() => console.log("50 ms later:", el.textContent), 50);
Console output:
right after render(): Loading cart...
50 ms later: Add to cart: $249.99 (0 in cart)

How does this code work?
createRoot(el) turns the div into a React root, and render() puts CartButton in it, wiping the placeholder text.
Notice the console: render() isn’t synchronous. Right after the call, the div still held the placeholder; 50 milliseconds later it held the button.

Important: call createRoot once per div. To update the UI, call root.render() again on the same root.
Method 2: Mount the Same Component into Multiple Divs
Same project, now the two .rating divs. Loop over them and give each its own root, passing the props from data-* attributes:
document.querySelectorAll<HTMLElement>(".rating").forEach((div) => {
const { label = "", stars = "0" } = div.dataset;
createRoot(div).render(<Rating label={label} stars={Number(stars)} />);
console.log("mounted rating:", label, stars);
});
Console output:
mounted rating: Tent 4.6
mounted rating: Rainfly 4.2

How does this code work?
dataset returns strings, so Number(stars) turns “4.6” into 4.6 for the prop. Every root updates on its own.
Pro Tip: separate roots don’t share context. If the widgets need shared state, one root that uses createPortal is cleaner, as in this React modal component.
Method 3: Create a Div, Mount into It, Then Unmount
Sometimes the div doesn’t exist yet, like a sale banner. Same Lakeside project; this code creates the div, mounts the banner and cleans up when you click Close:
const banner = document.createElement("div");
document.body.prepend(banner);
const bannerRoot = createRoot(banner);
bannerRoot.render(
<SaleBanner
onClose={() => {
bannerRoot.unmount();
banner.remove();
console.log("banner unmounted, div removed");
}}
/>,
);
Console output after clicking Close:
SaleBanner cleanup ran
banner unmounted, div removed
banner divs left: 0

How does this code work?
document.createElement builds the div and prepend adds it. On Close, root.unmount() runs every effect cleanup (that’s the “cleanup ran” line), then banner.remove() drops the empty div.
After unmount(), that root is finished; calling render() on it throws. Unmounting also prevents state updates on unmounted components.
Why Does createRoot Say the Target Container Is Not a DOM Element?
Because the div wasn’t found and getElementById returned null. I typed cart-widgit on purpose and ran a development build:
import { createRoot } from "react-dom/client";
import { CartButton } from "./App";
try {
const el = document.getElementById("cart-widgit") as HTMLElement;
createRoot(el).render(<CartButton price={249.99} />);
} catch (err) {
console.error((err as Error).message);
}
Console output:
[error] Target container is not a DOM element.
Usual causes: a typo in the id, or a script in <head> without defer that runs before the div exists.
TypeScript catches the null case earlier. Without a check, the project’s tsc reports:
error TS2345: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Container'.

Fix it with the if (!el) throw check from Method 1, which narrows the type. A non-null ! also compiles, but it only hides the problem.
Why ReactDOM.render No Longer Works in React 19
React 18 deprecated ReactDOM.render; React 19 removed it, along with hydrate and unmountComponentAtNode. Old code like this fails on React 19.3:
import ReactDOM from "react-dom";
import { CartButton } from "./App";
ReactDOM.render(<CartButton price={249.99} />,
document.getElementById("cart-widget"));
tsc output:
error TS2339: Property 'render' does not exist on type 'typeof import("C:/pyguides/reactapp/node_modules/@types/react-dom/index")'.
The fix is mechanical: render(el, node) becomes createRoot(node).render(el), and unmountComponentAtNode(node) becomes root.unmount(). For server-rendered HTML, use hydrateRoot(node, el) instead.
If nothing shows after the switch, work through why a React component is not rendering.
Which Method Should You Use?
| Situation | Use | Why |
|---|---|---|
| Whole app or one widget | Method 1: createRoot | One root, one div |
| Same widget in many spots | Method 2: root per div | Independent islands |
| Div doesn’t exist yet | Method 3: create + unmount | Full cleanup |
I’d start with Method 1. StrictMode wraps each root separately, and you can even turn StrictMode off for one component.
Things to Keep in Mind
- Import
createRootfromreact-dom/client, notreact-dom. - The first
render()clears the div, and React owns it afterward. - An unmounted root is done: create a new root to show the div again.
A few more React guides from the same series:
- Build a React calendar component with events
- Create a React drag and drop component
- Split a layout with React Mosaic
- Open a React component in a new tab
Frequently Asked Questions
How do I render a React component in a specific div?
Pass the div to createRoot, then call root.render(<Component />).
Can I have multiple React roots on one page?
Yes, one createRoot call per div; each root updates independently.
What replaced ReactDOM.render in React 18 and 19?
createRoot(node).render(element) from react-dom/client.
How do I unmount a React component from a div?
Call root.unmount() on the root you created.