🎓 A1Learner
🔍
0 of 50 lessons completed

Rendering a Component

Rendering a component means showing it on the page. createRoot is the tool that does that job.

The mount code

This code lives in main.jsx.

import { createRoot } from 'react-dom/client';
import App from './App.jsx';
createRoot(document.getElementById('root')).render(<App />);

The rendered result

App returns this heading for the page.

return <h1>Rendered by React</h1>

Example

export default function App() {
  return <h1>Rendered by React</h1>
}

Key points

  • createRoot is a function from the react-dom client package.
  • react-dom is the package that connects React to the browser page.
  • createRoot takes the page element with the id root.
  • The render call shows the App component inside that element.
  • The id root comes from the div in index.html.

In this example, App returns a heading. createRoot mounts App on the page. The render call puts the heading inside the root element. The page shows Rendered by React.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which function mounts a React app on the page?