The ReactDOM render process shows how React puts your components on the web page. It helps you see your app's content in the browser.
ReactDOM render process
import ReactDOM from 'react-dom/client'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<App />);
Use ReactDOM.createRoot to start rendering in React 18+.
The render method shows the React component inside the chosen HTML element.
import ReactDOM from 'react-dom/client'; const root = ReactDOM.createRoot(document.getElementById('app')); root.render(<h1>Hello World</h1>);
MyComponent inside the element with id 'root'.import ReactDOM from 'react-dom/client'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<MyComponent />);
This example creates a simple React component called Greeting that shows a welcome message. Then it uses ReactDOM's render process to display it inside the HTML element with id 'root'.
import React from 'react'; import ReactDOM from 'react-dom/client'; function Greeting() { return <h2>Welcome to React!</h2>; } const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<Greeting />);
Always make sure the HTML element you target (like root) exists in your HTML file.
ReactDOM's render process replaces the content inside the target element with your React component.
In React 18+, use ReactDOM.createRoot instead of the old ReactDOM.render.
ReactDOM render process shows your React components on the web page.
Use ReactDOM.createRoot and root.render() to display components.
Target an existing HTML element to place your React app content.
Practice
ReactDOM.createRoot in React?Solution
Step 1: Understand ReactDOM.createRoot role
This function creates a root container that connects React with a specific DOM element.Step 2: Connect React components to the DOM
Using this root, React can render components inside the chosen HTML element.Final Answer:
To create a root container to render React components into the DOM -> Option CQuick Check:
ReactDOM.createRoot creates root container [OK]
- Confusing createRoot with component creation
- Thinking it updates state
- Assuming it fetches data
App inside a DOM element with id root using React 18+?Solution
Step 1: Identify React 18+ render syntax
React 18+ uses ReactDOM.createRoot with root.render to mount components.Step 2: Match correct method calls and parameters
const root = ReactDOM.createRoot(document.getElementById('root')); root.render(); correctly calls createRoot with the DOM element, then calls render with the component.Final Answer:
const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<App />); -> Option AQuick Check:
ReactDOM.createRoot + root.render = correct syntax [OK]
- Using old ReactDOM.render syntax in React 18+
- Swapping parameters in createRoot or render
- Calling methods in wrong order
const root = ReactDOM.createRoot(document.getElementById('root'));
function Greeting() {
return <h1>Hello, React!</h1>;
}
root.render(<Greeting />);Solution
Step 1: Understand component definition and rendering
The Greeting function returns an h1 element with text 'Hello, React!'.Step 2: ReactDOM renders Greeting component inside 'root'
Calling root.render with <Greeting /> displays the returned h1 content in the page.Final Answer:
Hello, React! inside an h1 heading -> Option AQuick Check:
Component returns h1 with text, so page shows it [OK]
- Thinking component name shows as text
- Expecting error without import
- Assuming empty page without render
const root = ReactDOM.createRoot('root');
root.render(<App />);Solution
Step 1: Check createRoot argument type
createRoot expects a DOM element, but 'root' is a string, not an element.Step 2: Correct usage requires document.getElementById('root')
We must pass document.getElementById('root') to get the actual DOM node.Final Answer:
The argument to createRoot should be a DOM element, not a string -> Option DQuick Check:
createRoot needs DOM element, not string [OK]
- Passing string id instead of element
- Calling render before createRoot
- Thinking component type matters here
Dashboard inside a div with id app-root. You also want to ensure the component updates correctly when state changes. Which code snippet correctly sets up the ReactDOM render process for this scenario?Solution
Step 1: Select the correct DOM element
Use document.getElementById('app-root') to get the div element for rendering.Step 2: Use ReactDOM.createRoot and root.render properly
Call createRoot with the element, then render the component as JSX (<Dashboard />) to enable React updates.Step 3: Avoid passing string or calling component as function
Passing string or calling Dashboard() directly breaks React's rendering and update process.Final Answer:
const root = ReactDOM.createRoot(document.getElementById('app-root')); root.render(<Dashboard />); -> Option BQuick Check:
createRoot + render with JSX component = correct [OK]
- Passing string instead of element to createRoot
- Using old ReactDOM.render in React 18+
- Calling component as function instead of JSX
Ready to prove it?
Pass the test and this pattern moves unknown
