Discover how React turns tedious page updates into smooth, automatic changes with just a few lines of code!
Creating first React app - Why You Should Know This
Imagine building a website where every time you want to change a button color or update a list, you have to find the exact spot in your HTML and rewrite it manually.
For example, changing a greeting message means hunting through the page code and editing it everywhere it appears.
Manually updating HTML is slow and easy to mess up.
You might forget one place, causing inconsistent content.
It's hard to keep track of what changed, and the page might flicker or reload fully, making the experience clunky.
Creating a React app lets you write small pieces called components that manage their own content and update automatically when data changes.
This means you write your UI once, and React handles showing the right thing on the screen smoothly and quickly.
<div id="greeting">Hello, User!</div> document.getElementById('greeting').innerText = 'Hello, Alice!';
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
// Usage example:
<Greeting name="Alice" />React makes building interactive, dynamic user interfaces easy and reliable, so your app feels fast and polished.
Think of a social media app where your friend list updates instantly when someone comes online, without you refreshing the page.
Manual HTML updates are slow and error-prone.
React components update UI automatically when data changes.
This leads to faster, smoother, and easier-to-maintain apps.
Practice
ReactDOM.createRoot method in a React app?Solution
Step 1: Understand ReactDOM.createRoot usage
This method is used to select the HTML element where the React app will appear, usually a div with id 'root'.Step 2: Identify its role in rendering
It creates a root for React to manage the app's UI inside that HTML element.Final Answer:
It tells React where to put the app in the HTML page. -> Option BQuick Check:
ReactDOM.createRoot sets the app's HTML container [OK]
- Thinking it creates components
- Confusing it with styling or data fetching
- Assuming it runs app logic
App?Solution
Step 1: Check function syntax for React component
A React functional component is a function that returns JSX, so it needs parentheses after the name.Step 2: Validate JSX return and syntax
function App() { returnHello; } correctly defines a function named App that returns JSX inside a div.Final Answer:
function App() { returnHello; } -> Option CQuick Check:
Functional component = function with () and JSX return [OK]
- Missing parentheses after function name
- Assigning JSX directly to a variable without function
- Using class syntax incorrectly for functional components
import React from 'react';
import ReactDOM from 'react-dom/client';
function App() {
return Welcome to React!
;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);Solution
Step 1: Understand what the App component returns
The App function returns an <h1> element with the text 'Welcome to React!'.Step 2: Check rendering process
The root renders the App component inside the HTML element with id 'root', so the heading will appear on the page.Final Answer:
Welcome to React! -> Option DQuick Check:
App returns <h1>Welcome to React!</h1> [OK]
- Thinking component name shows instead of its return
- Assuming error without checking code carefully
- Confusing JSX tags with plain text
import React from 'react';
import ReactDOM from 'react-dom/client';
function App() {
return <h1>Hello World</h1>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(App);Solution
Step 1: Check how render is called
React expects a JSX element or component instance in render, so is correct, not just App.Step 2: Understand difference between component and element
App is a function, is JSX that creates an element from that function.Final Answer:
App should be called as <App /> in render, not just App. -> Option AQuick Check:
render needs JSX element like <App /> [OK]
- Passing function name instead of JSX element to render
- Confusing component function with element instance
- Ignoring JSX syntax rules
div with a class container. Which code correctly creates and renders this app?Solution
Step 1: Use correct JSX attribute for class
In React, useclassNameinstead ofclassfor CSS classes.Step 2: Render the component as JSX element
Useroot.render(<App />)to render the component, not just the function name.Final Answer:
function App() { return <div className="container">Hello!</div>; } const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<App />); -> Option AQuick Check:
Use className and render JSX element [OK]
- Using class instead of className in JSX
- Passing function name instead of JSX element to render
- Using old ReactDOM.render instead of createRoot
Ready to prove it?
Pass the test and this pattern moves unknown
