← Back to Chapters

Rendering Elements in React

⚛️ Rendering Elements in React

? Quick Overview

In React, rendering means converting React elements (written in JSX or plain JavaScript) into actual HTML elements on the webpage. This process is done efficiently using the Virtual DOM, so only the parts that change are updated in the real DOM.

In React 18+, rendering is controlled mainly using ReactDOM.createRoot() and root.render() from the react-dom/client library.

Virtual DOM ➜ Compare ➜ Update Real DOM

? Key Concepts

  • React Element: A description of what you want to see on the screen (written in JSX).
  • Root Container: A special DOM node (usually <div id="root">) where your entire React app lives.
  • ReactDOM.createRoot(): Creates a root where React can control rendering.
  • root.render(): Tells React what to show inside the root container.
  • Virtual DOM: A lightweight copy of the real DOM that React uses to detect changes efficiently.

⚙️ ReactDOM.createRoot()

ReactDOM.createRoot() is used in React 18+ to create a root container. It tells React where in the HTML document the UI should be rendered.

? View Code Example
// Create a root container in React 18+
const root = ReactDOM.createRoot(document.getElementById("root"));

Here, "root" refers to the <div id="root"></div> element in your main index.html file. This is the entry point where React will control the UI.

? View HTML Root Element
<!-- Root element in index.html -->
<div id="root"></div>

? root.render()

Once the root container is created, the root.render() method is used to render React elements or components into that root.

? View Code Example
// Rendering a simple element
root.render(<h1>Hello, React Rendering!</h1>);

// Rendering a component
root.render(<App />);

Whenever the data changes, React recalculates what the UI should look like and updates only the changed parts of the real DOM. This makes React apps smooth and efficient, without full page reloads.

? How Rendering Works Internally

  • 1️⃣ React reads the JSX or element structure you wrote.
  • 2️⃣ It creates a Virtual DOM representation of the UI.
  • 3️⃣ React compares the new Virtual DOM with the previous one (diffing).
  • 4️⃣ Only the changed elements are updated in the real DOM.
  • 5️⃣ The browser shows the updated UI, without a full page refresh.

? Complete Rendering Example

Below is a typical index.js file in a React 18 project that uses ReactDOM.createRoot() and root.render() to display the App component.

? View index.js Example
// index.js - Entry point of a React app
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

// Create a root container using the <div id="root">
const root = ReactDOM.createRoot(document.getElementById("root"));

// Render the App component inside React.StrictMode
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

? Explanation / Live Output

  • ReactDOM.createRoot(...) connects React to the DOM element with id="root".
  • root.render(...) tells React what to show inside that root (here, the <App /> component).
  • <React.StrictMode> helps you catch potential problems early during development.
  • In the browser, you will see whatever UI is defined inside the App component.

? Tips & Best Practices

  • In React 18+, always use ReactDOM.createRoot() instead of the old ReactDOM.render().
  • Keep only one root container for your main app (usually id="root").
  • Wrap your main component in <React.StrictMode> for better debugging and warnings in development.
  • Make sure the <div id="root"> element actually exists in your index.html file.
  • Remember: root.render() can render a single element, multiple elements, or entire component trees.

? Try It Yourself

  1. Create a new React app (for example using create-react-app or Vite).
  2. Open the index.js (or main.jsx) file in the src folder.
  3. Modify the rendering code so it temporarily renders: “Learning React Rendering!” inside an <h1> element.
  4. Create a new component called Message that returns <p>React is updating this efficiently!</p> and render it inside root.render() along with App.
  5. Observe how the page updates instantly in the browser without a full reload when you save changes.

Goal: Understand clearly how ReactDOM.createRoot() and root.render() work together to display and update React components efficiently inside the browser.