Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 5

Rendering Components to the DOM

Recommended resources

Hello!

In our last lesson, you learned how to create your first functional components—the reusable building blocks of React applications. We saw how to define a component as a JavaScript function that returns JSX, and how to compose them inside our main App component.

This leaves one crucial question: how does our App component, which lives in a JavaScript file, actually get displayed as HTML in the browser?

Today's lesson connects the final dots in the rendering pipeline. You will learn how to render a React component to the DOM. This is the process that takes your abstract React components and makes them tangible, visible elements on a web page.

The Bridge Between React and the Browser

So far, we've talked a lot about "React". But if you look in your project's package.json file, you'll see two key dependencies: react and react-dom.

  • react is the core library that defines components, state, and other fundamental building blocks. It's platform-agnostic, meaning it doesn't know or care about browsers or mobile devices.
  • react-dom is the "renderer" specifically for the web. It acts as the bridge, providing the tools to translate your React components into the browser's Document Object Model (DOM).

The Entry Point: index.html and main.jsx

Every React application built with Vite starts with two key files:

  1. public/index.html: This is the only HTML file in your project. If you open it, you'll find it's mostly empty, but it contains one critical line:

    <div id="root"></div>
    

    This div is the container. Your entire React application will be "injected" or "mounted" inside this element.

  2. src/main.jsx: This is the JavaScript entry point for your application. It’s the script that's executed first, and its job is to tell React what to render and where to render it.

To see how these files work together, let's watch a short video segment.

React Tutorial for Beginners

This clip from Programming with Mosh's tutorial clearly explains the roles of index.html, main.tsx (the TypeScript equivalent of main.jsx), and react-dom in the initial rendering process.

Watch from 16:38 to 19:06. Pay close attention to how ReactDOM.createRoot() targets the <div id="root"> and how root.render() is used to place the <App /> component inside it.


The Rendering API: createRoot and render

As the video showed, the magic happens in src/main.jsx using two main functions from the react-dom/client library. Let's break them down.

Rendering in React

This article from BigBinary Academy provides a clear, step-by-step explanation of the functions used for rendering. We will focus on the introduction and the sections explaining createRoot() and render().

Read the article from the beginning down to the end of the 'render() Method' section. This will give you a solid textual explanation of the code you just saw in the video.

Based on that reading, let's summarize the three-step process:

  1. Find the DOM container: We use standard browser JavaScript to get a reference to our host element.

    const domNode = document.getElementById('root');
    
  2. Create a React root: We pass this DOM element to createRoot(). This tells React to take control of managing the DOM inside domNode.

    import { createRoot } from 'react-dom/client';
    
    const root = createRoot(domNode);
    
  3. Render your component: Finally, we call the render() method on the root and pass the React component we want to display. This is typically your main <App /> component.

    import App from './App.jsx';
    
    root.render(<App />);
    

    React then takes the JSX returned by <App /> (and all its nested components), converts it into DOM nodes, and appends them to the <div id="root">.

The complete main.jsx file usually looks very clean and simple, like this:

Rendering a React Component to the DOM
This code shows the complete process: importing `createRoot`, importing the `MyComponent`, getting the root DOM element, creating the React root, and finally rendering the component into that root.

The official React documentation provides the definitive reference for this process. It's a good practice to familiarize yourself with it. The "Rendering an app fully built with React" section is particularly relevant.

createRoot

The official React docs offer the most precise explanation. Skim through these sections to solidify your understanding of the API and its usage.

Read the sections 'createRoot(domNode, options?)', 'root.render(reactNode)', and 'Rendering an app fully built with React'. Note the clear separation between creating the root and rendering into it.

Test your understanding!

Imagine you have the standard index.html file with <div id="root"></div>. What would happen if your main.jsx file contained this line?

const domNode = document.getElementById('app-root');
const root = createRoot(domNode);
root.render(<App />);

What error would you see in the browser's developer console, and why?

Show answer

You would likely see an error similar to: Uncaught TypeError: Cannot read properties of null (reading '...') or Target container is not a DOM element.

This happens because document.getElementById('app-root') returns null, since there is no element with the ID app-root in your index.html file. You are then trying to pass null to createRoot(), which is not a valid DOM element, causing the application to crash. This highlights the critical link between the ID in your HTML and the ID used in your JavaScript.


What Does "Rendering" Actually Mean?

We've used the term "rendering" to mean "making something appear on screen". For a developer, it's useful to have a slightly deeper mental model. In React, rendering is the process of converting your component code into the actual browser DOM elements.

This video provides a great conceptual walkthrough of that process.

Understanding this React concept will make you a pro React developer!

This short video from Code Sketched explains what happens under the hood when React 'renders' a component. It introduces the idea of an 'element definition,' which is React's internal object representation of your UI.

Watch from 00:43 to 02:49. Focus on how React recursively breaks down your components into a tree of simple element descriptions (like a big JSON object) before creating the final DOM structure.

This idea of React building a description of the UI first is central to its efficiency. When your application's data changes, React can create a new description, compare it to the old one, and efficiently update only the parts of the actual DOM that have changed. We'll explore this much more when we get to the useState hook.


Conclusion

You have now completed the entire journey from writing a component to seeing it on the screen! This is a fundamental part of how every React application works.

Let's recap the key takeaways:

  • The public/index.html file provides the host page and a root DOM element, typically <div id="root"></div>.
  • The src/main.jsx file is the entry point where your React application is initialized.
  • The react-dom library provides the tools to connect your React components to the browser's DOM.
  • The process involves using ReactDOM.createRoot() to designate a DOM element as the root, and then calling root.render() to display your component tree inside it.

Now that we understand how to create and display components, our next step is to make them dynamic and reusable. In the next module, we'll start by learning how to pass data into components using a mechanism called props. This will allow you to customize a component's appearance and behavior each time you use it.