Why a plain variable isn’t enough
In Level 2, you built a counter by rewriting the button’s text yourself after each click. React does it differently: you change the data, and React redraws the screen. But you have to hand the data over to a special box called state, otherwise React won’t notice the change.
Show me: a counter in React
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicked: {count}×
</button>
);
}
export default Counter;
Breaking down the most important line: const [count, setCount] = useState(0);:
useState(0): “React, set up a box for me with a starting value of 0.”count: the current value in the box (read-only).setCount: the only correct way to change the value. When you callsetCount(5), React saves the new value and redraws everywherecountis used, on its own.
And onClick={...} is the React version of addEventListener("click", ...), right inside JSX.
Notice what’s not in the code: no getElementById, no textContent. You don’t say how to change the screen; you say what’s true now (count is now 5), and the screen lines itself up on its own.
Show me: state controls what’s visible
State doesn’t have to be just a number. A collapsible section, the React way:
import { useState } from 'react';
function Menu() {
const [open, setOpen] = useState(false);
return (
<div>
<button onClick={() => setOpen(!open)}>
{open ? "Hide the menu ▲" : "Show the menu ▼"}
</button>
{open && (
<ul>
<li>Monday: tomato soup with pasta</li>
<li>Tuesday: chicken with rice</li>
<li>Wednesday: pancakes with jam</li>
</ul>
)}
</div>
);
}
Two new JSX tricks:
condition ? A : B: shorthand if/else right inside HTML: “if it’s open, show this, otherwise that.”condition && (...): “only render this if the condition is true.” Whenopenis false, the list simply doesn’t exist.
And !open means “the opposite”: it turns true into false and vice versa. One line = a switch.
Your turn 💪
Add two things with state to your app:
- A collapsible section (a
true/falsestate) matching your project: 🏫 a collapsible lunch menu · 🙋 “show/hide my top games” · 🌍 a collapsible list of countries · ⚽ a collapsible roster. - A counter or a vote (a numeric state): 🏫 vote for the class trip · 🙋 “how many people greeted me today” · 🌍 visited countries counter · ⚽ a scoreboard with
+1 home/+1 awaybuttons (two states!).
Bonus solution: a scoreboard with two states
import { useState } from 'react';
function Score() {
const [home, setHome] = useState(0);
const [away, setAway] = useState(0);
return (
<div className="card">
<h3>Riverside {home} : {away} Lakeside</h3>
<button onClick={() => setHome(home + 1)}>Home goal ⚽</button>
<button onClick={() => setAway(away + 1)}>Away goal 😤</button>
<button onClick={() => { setHome(0); setAway(0); }}>New match</button>
</div>
);
}
export default Score;
A component can have as many boxes as it wants: each useState is one.
Check yourself: Your app has at least two interactive spots, and there’s no getElementById anywhere in the code. If you’ve mastered this mental shift (“I change the data, not the screen”), you’ve mastered the hardest part of React.
What to take away from this lesson
const [value, setValue] = useState(start): a component’s memory.- Change it only through
setValue(...): React then redraws on its own. - In JSX:
onClick={...}, conditions? :and&&control what’s visible.
© 2026 Ing. Martin Polak / AlgoRhino · Content usage terms