Explain it simply
Explain it simply
In a taxi you can give turn-by-turn directions: "left here, right at the lights, straight for two hundred metres". Or you can just name the destination: "Sule Pagoda, please". Editing the DOM with plain JavaScript is the first style; React is the second.
In React you describe the destination, what the screen should look like, with components, and React works out the route: how to change the DOM to get there. React was created at Meta (then Facebook) and is now maintained in the open with a large community.
Problem → Why → Solution
The problem: instructing the DOM through every transition
Think about a notification badge. With no messages the badge must be hidden; with messages it must be visible and show the count. Imperative code has to handle every transition itself: 0 to 1, 1 to 2, 2 back to 0. The code below forgets the path back to 0, so the badge still shows "1" after every message has been read.
const badge = document.querySelector("#badge");
function setUnread(count) {
if (count > 0) {
badge.style.display = "inline";
badge.textContent = count;
}
// count === 0: nobody hides the badge or clears the old number
}
setUnread(1);
setUnread(0); // the badge still says "1"Why does this happen?
Imperative code only issues "what to change" instructions without a single description of what the screen should be right now. The more states you have, the more transitions you must handle. In React you write one description of the badge for any count, such as {count > 0 && <span>{count}</span>}, and React works out every transition.
Core concept: declarative UI, the component tree and two packages
A React component never touches the DOM directly. It returns a description, a React element, that says "for this data, the UI looks like this". That is what declarative UI means. Components can contain other components, forming a tree such as App, then Header, then NavLink. The component at the top, usually App, is called the root component.
A React element is not a DOM node. It is a small JavaScript object that records a type, such as "h1", and props, such as children: "Hello". Creating objects is cheap, which is why React can afford to produce a fresh description of the UI on every render and compare it with the last one.
React is split into two packages. The react package holds what you need to write components, such as hooks like useState and StrictMode, and knows nothing about any platform. The react-dom package is the renderer that actually writes those descriptions into the browser DOM. That split is why React Native can reuse the same react package with a different renderer to draw on mobile screens. In a browser app you import createRoot() from react-dom/client.
createRoot(domNode) claims an empty element in index.html, such as <div id="root"></div>, as the area React will manage. Calling root.render(<App />) then happens in two steps. In the render step React calls App (and every component inside it) to get the complete tree of UI descriptions. In the commit step it creates or changes DOM nodes so the page matches that tree.
Later, when data changes and React renders again, it compares the new description with the previous one and changes only the parts of the DOM that actually differ. That is how updates stay fast without rebuilding the whole page.
- Components describe the UI
- Render: React calls the components
- Commit: react-dom updates the DOM
- Next time: only differences change
Components say what; React decides how.
General pattern: mounting an app on the page
import { createRoot } from "react-dom/client";
function RootComponent() {
return <element>...</element>;
}
const root = createRoot(document.getElementById("container-id"));
root.render(<RootComponent />);An entry file calls createRoot() once for the whole app. container-id is the id of the empty element in index.html, and RootComponent is the component at the top of the tree.
Example: putting Hello, React! on the page
This file is the smallest possible React app: declare a component, create a root, render the component. It assumes index.html already contains <div id="root"></div>. As you read, mark which lines belong to the react package and which to react-dom; that split is the main idea of this lesson.
import React from 'react';
import ReactDOM from 'react-dom/client';
function App() {
return <h1>Hello, React!</h1>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);Code breakdown
- Line 1
import React from 'react';Since React 17's new JSX transform, tools such as Vite no longer need React imported for JSX. You can delete this line and the app still works; it is shown because you will see it in older code.
- Line 2
import ReactDOM from 'react-dom/client';react-dom/client is the browser renderer's entry point. Modern code usually writes the named import instead: import { createRoot } from 'react-dom/client';
- Lines 4-6
return <h1>Hello, React!</h1>;The App component returns a React element describing one <h1>. Nothing in the DOM changes at this point; it is only a description.
- Line 8
const root = ReactDOM.createRoot(document.getElementById('root'));The element with id="root" becomes the container React manages. If no element has that id, getElementById returns null and React throws "Target container is not a DOM element."
- Line 9
root.render(<App />);<App /> creates an element for the App component. Only when render() is called does React call App (render) and insert the resulting <h1> into the DOM (commit).
A `Hello, React!` heading will appear in the browser.Imperative and declarative side by side
Rewriting the badge from the problem section in React makes the code much shorter. The component takes count and describes the badge for every possible count in a single expression. When count is 0 the expression evaluates to false and React shows nothing, so no separate hiding code is needed.
function Badge({ count }) {
return <span className="badge-slot">{count > 0 && <b>{count}</b>}</span>;
}
// <Badge count={0} /> renders <span class="badge-slot"></span>
// <Badge count={2} /> renders <span class="badge-slot"><b>2</b></span>| Question | Imperative (plain DOM) | Declarative (React) |
|---|---|---|
| What you write | Steps for every transition | The UI for any given data |
| Who edits the DOM | Your code | react-dom |
| Adding a new state | More transitions to handle | Adjust one description |
In real projects
In real projects
Every React app built with Vite or a framework has an entry file like this example; in Vite it is src/main.jsx. Usually createRoot() is called once for the whole app, and all other UI lives inside App's component tree. Frameworks such as Next.js write this entry file for you, so you never see it there. Knowing what it does still helps when a blank page needs debugging.
Some companies do not rewrite an old website at all; they build one widget, such as a comment box, in React. They point createRoot() at an element like <div id="comments"></div> and introduce React gradually. Because react and react-dom are separate, tests can render components without a real browser, using react-dom/server or a testing library.
Common mistakes
Common mistakes
1. A container id that does not match
If index.html has <div id="app"> but the script asks for getElementById('root'), the result is null, React throws "Target container is not a DOM element." and the page stays blank. Check that both ids are spelled the same.
2. Still calling ReactDOM.render()
Old tutorials call ReactDOM.render(<App />, container) from 'react-dom'. It was deprecated in React 18 and removed in React 19, so it no longer works. Use createRoot() from react-dom/client.
3. Mixing React with direct DOM edits
Editing innerHTML inside an element React manages, via document.querySelector(), makes the DOM disagree with React's description. On the next render your change may vanish or cause errors. Change the UI by changing data instead.
Best practices
Best practices
- Import createRoot by name from react-dom/client and create only one root per app.
- Wrap <App /> in <StrictMode> during development; it surfaces impure component code early.
- Never edit the DOM inside an element React manages; change the data instead.
Mini challenge
Mini challenge: mount a Today card
Write an entry file (main.jsx). Declare a TodayCard component and render it into the root element using the named createRoot import and StrictMode.
- Import createRoot from react-dom/client with a named import
- TodayCard uses a day variable and a tasks array to show a heading and two lines
- Use tasks.length and tasks[0] inside braces
- Wrap <TodayCard /> in <StrictMode> when rendering
// TODO: import StrictMode from "react" and createRoot from "react-dom/client"
function TodayCard() {
// TODO: const day = ...; const tasks = [...];
return (
<article className="today">
{/* TODO: "Today is <day>" heading */}
{/* TODO: "You have N tasks left." */}
{/* TODO: "Next up: <first task>" */}
</article>
);
}
// TODO: create a root on #root and render <TodayCard /> inside <StrictMode>Expected: The page shows the heading "Today is Monday" and the lines "You have 3 tasks left." and "Next up: Read the JSX lesson".
Show hint
The first array item is tasks[0]. You can chain .render() directly on the root returned by createRoot(...).
Show solution
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
function TodayCard() {
const day = "Monday";
const tasks = ["Read the JSX lesson", "Finish the quiz", "Push to GitHub"];
return (
<article className="today">
<h2>Today is {day}</h2>
<p>You have {tasks.length} tasks left.</p>
<p>Next up: {tasks[0]}</p>
</article>
);
}
createRoot(document.getElementById("root")).render(
<StrictMode>
<TodayCard />
</StrictMode>
);Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- React is declarative: components describe the UI and React decides how to change the DOM.
- The react package provides components and hooks; react-dom writes them into the browser DOM.
- createRoot(container).render(<App />) mounts the app on the root element in index.html.
- Render calls the components, commit updates the DOM, and later renders change only the differences.
React Versions — React