← Level 3 – Master

Lesson 5 of 10 ⏱ 40 minutes Premium

State: an app that remembers

Learn useState: a component's memory. Your app starts reacting to clicks the React way.

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);:

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:

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:

  1. A collapsible section (a true/false state) matching your project: 🏫 a collapsible lunch menu · 🙋 “show/hide my top games” · 🌍 a collapsible list of countries · ⚽ a collapsible roster.
  2. 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 away buttons (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