BeginnerCoder

Lesson 3 — State & useState

State remembers between renders

A normal variable resets every time the component runs again. State is a value React keeps for you. useState(0) — a hook, a special React function whose name starts with use — returns a pair: the current value and a setter function. Calling the setter stores the new value and makes React re-render (run the component again and update the screen).

const [likes, setLikes] = useState(0);
<button onClick={() => setLikes(likes + 1)}>♥ {likes}</button>

Why it matters: anything that changes on screen — counters, forms, open/closed menus — lives in state.

Want it another way?
Task

Make the click add 1 to n. After two clicks the button should read "Clicked 2".

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.