In this lesson you will
- Explain why changing a normal variable does not update the screen
- Add state with useState and change it from onClick handlers
- Use updater functions when the next value depends on the previous one
- Update objects and arrays in state by copying, not mutating
Why a normal variable is not enough
Try the obvious approach: let count = 0, and a button whose onClick runs count++. The variable does go up, as the Console shows, but the screen stays at 0. Two things are missing. First, nothing tells React that something changed, so it does not render the component again. Second, even if it did, rendering means calling your function again, and let count = 0 would start from zero each time. Local variables do not survive between renders.
useState solves both. React keeps the value for you, outside your function, and gives it back on every render. The setter function stores a new value and schedules a new render. Event handlers in JSX look like HTML ones but take a function, not a string: onClick={handleAdd} or an inline arrow onClick={() => setCount(count + 1)}. A classic slip is onClick={handleAdd()} with brackets, which calls the function during rendering instead of on click.
Console
- Console messages, errors, form data and clicked links appear here.
Click both buttons a few times. The first logs a growing number while still showing 0. The second updates the screen, and the Console shows React calling WorkingCounter again after every click with the new value.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is a Hook in React?
A Hook is a function whose name starts with use that lets a component use a React feature, such as state with useState or side effects with useEffect. Hooks only work inside components or other Hooks, at the top level.
Why does my React component render twice in development?
If your app is wrapped in <StrictMode>, React calls components twice in development to help you find impure code. It does not happen in production. The examples here do not use StrictMode, so each log appears once.
Does setState update the value immediately?
No. It stores the new value for the next render. In the current handler the variable keeps its old value. Use an updater function, or compute the new value in a variable first if you need it straight away.
Should I keep one big state object or many useState calls?
Prefer separate useState calls for values that change independently. Group values into one object only when they always change together, such as the x and y of a position. The React docs give this advice in "Choosing the State Structure".
Is state lost when I refresh the page?
Yes. State lives in memory while the page is open. To keep data after a refresh, save it somewhere else, such as localStorage or a server, and read it back when the component starts. The project at the end of this course does exactly that.
Sources
Facts in this lesson were checked against these official pages on 19 November 2026.
- React docs: State, a Component’s Memory
- React reference: useState
- React docs: State as a Snapshot
- React docs: Queueing a Series of State Updates
- React docs: Updating Objects in State
- React docs: Rules of Hooks
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.