Sub topic 1
What React is and what it is for
Prerequisites
This chapter assumes you know basic HTML and CSS, and JavaScript fundamentals: functions, objects, arrays, and ES modules. You don't need to install anything yet. Later chapters use a terminal with Node.js and npm.
What React is
React is a JavaScript library for building user interfaces. A user interface (UI) is everything a person sees and interacts with on a screen. React does one job: it keeps the page in sync with your data. It does not include a router, a data-fetching layer, or a build system. You add those separately.
You write small pieces called components. A component is a JavaScript function that returns a description of part of the screen. You combine components to build a whole page.
The problem React solves
Without a library, you update a page by hand. You find an element, then change its text, class, or children. In a small page this is fine. In a large page, many pieces of data affect many elements, and it is easy to forget one. The screen then disagrees with your data.
React removes this step. You describe what the screen should look like for the current data, and React updates the real page (the DOM, or Document Object Model) to match.
UI as a function of state
State is data that can change while the app runs, such as a counter value or the text in a form field. In React, the screen is a result of that state:
When state changes, React calls your component again. It compares the new description with the previous one and changes only the parts of the DOM that differ. You never write the update steps yourself. This style is called declarative: you state the result you want, not the steps to get there.
A first look at a component
The following component shows the idea. You don't need to run it yet. The setup comes in later chapters.
import { useState } from 'react'
function Greeting() {
const [name, setName] = useState('')
return (
<div>
<input value={name} onChange={(e) => setName(e.target.value)} />
<p>Hello, {name || 'stranger'}!</p>
</div>
)
}
export default GreetingThe <p> line is never edited by hand. When the user types, setName changes the state, React calls Greeting again, and the paragraph shows the new text. The tag-like syntax is JSX, which a later chapter covers.
What React is good for
- Interactive pages where data changes often, such as dashboards, forms, and editors.
- Large interfaces that you want to split into reusable parts.
- Single-page apps, where the browser loads one page and React changes its content as the user navigates.
React is less useful for a mostly static page with a few small interactions. Plain HTML or a static site generator is simpler there.
Common misunderstandings
- React is a library, not a full framework. Routing, data loading, and server rendering come from other tools or from frameworks built on React.
- A component re-running does not mean the whole page is rebuilt. React updates only what changed in the DOM.
- Regular local variables do not survive between renders. Use state for values that must be remembered.
Exercise
In React, what does it mean that a UI is "a function of state", and who performs the DOM updates?
Show answer
The screen is computed from the current state: for the same state, a component describes the same UI. When state changes, React calls the component again, compares the result with the previous one, and updates the DOM itself. Your code only describes the result.
Exercise
A page shows a count in a <span>. A developer changes a plain JavaScript variable on click, and the span does not update. Why?
Show answer
A plain variable change does not tell React to render again, and the variable is not kept between renders. The count must be stored with useState, and the update must go through its setter function so React re-runs the component.