Rendering elements means showing things on the screen using React. It helps you display text, images, or other content in your app.
When you want to show a greeting message on a webpage.
When you need to display a list of items like tasks or products.
When you want to update what the user sees after they click a button.
When you want to show different content based on user actions.
When you want to build a user interface that changes over time.
Syntax
React
const element = <tag>content</tag>;
ReactDOM.createRoot(document.getElementById('root')).render(element);
Use JSX syntax inside <tag></tag> to create elements.
The render method shows the element inside the chosen HTML container.
Examples
This shows a big heading with the text "Hello, world!" on the page.
React
const element = <h1>Hello, world!</h1>;
ReactDOM.createRoot(document.getElementById('root')).render(element);
This renders a simple paragraph on the screen.
React
const element = <p>This is a paragraph.</p>;
ReactDOM.createRoot(document.getElementById('root')).render(element);
This shows a clickable button labeled "Click me".
React
const element = <button>Click me</button>;
ReactDOM.createRoot(document.getElementById('root')).render(element);
Sample Program
This React code creates a heading element and shows it inside the HTML element with id "root".
React
import React from'react';
import ReactDOM from'react-dom/client';
const element = <h2>Welcome to React!</h2>;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(element);
OutputSuccess
Important Notes
Always make sure your HTML container (like a div with id "root") exists in your HTML file.
JSX looks like HTML but it is JavaScript under the hood.
React updates the screen efficiently when elements change.
Summary
Rendering elements means showing content on the screen using React.
Use JSX syntax to create elements and render to display them.
This is the first step to building interactive React apps.
Practice
(1/5)
1. What does rendering elements in React mean?
easy
A. Managing database connections
B. Writing CSS styles for components
C. Creating backend APIs
D. Showing content on the screen using React components
Solution
Step 1: Understand rendering in React
Rendering means React creates and shows elements on the screen for users to see.
Step 2: Identify the correct description
Only showing content on the screen matches rendering elements, not styling or backend tasks.
Final Answer:
Showing content on the screen using React components -> Option D
Quick Check:
Rendering = Showing content [OK]
Hint: Rendering means displaying content on screen [OK]
Common Mistakes:
Confusing rendering with styling
Thinking rendering means backend work
Mixing rendering with database tasks
2. Which of the following is the correct way to render a simple React element?
easy
A. ReactDOM.render(<h1>Hello</h1>, document.getElementById('root'));
B. React.render(<h1>Hello</h1>, 'root');
C. ReactDOM.render('Hello', document.getElementById('root'));
D. ReactDOM.render(<h1>Hello</h1>);
Solution
Step 1: Recall ReactDOM.render syntax
The correct syntax requires ReactDOM.render(element, container), where container is a DOM node.
Step 2: Check each option
ReactDOM.render(<h1>Hello</h1>, document.getElementById('root')); uses ReactDOM.render with JSX element and document.getElementById('root'), which is correct.
Final Answer:
ReactDOM.render(<h1>Hello</h1>, document.getElementById('root')); -> Option A