React · Lesson 3 of 15 · 4 min
Managing Component State with useState
This is the first thing an interviewer will ask you to code. Get it right and you look competent; get it wrong and the interview is over.
This lesson gives you the mental model to explain how state works and the patterns to write bug-free components. You will be able to explain why a simple variable doesn't work, what useState actually does, and how to handle asynchronous updates correctly.
Why State Exists: To Trigger Re-renders
A component is just a function that returns JSX. React calls this function to figure out what to draw on the screen. If you want to change what's on the screen, you need to tell React to call that function again with new data.
A normal JavaScript variable won't do this. If you declare let count = 0; inside your component and increment it in a click handler, the value of count changes, but React has no idea. Nothing happens on the screen.
State is your signal to React. When you declare a piece of state, you're saying: "Track this value. When it changes, re-render this component and any children that depend on it."
The Mechanics of useState
useState is a hook that lets you declare a state variable. You call it at the top level of your component.
It takes one argument: the initial value of the state. It returns an array containing exactly two things: the current value of the state, and a function to update it.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
// Request a re-render with a new state value
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
When Counter first renders, useState(0) initializes the state. count is 0. When the button is clicked, increment calls setCount(1). This does two things:
- It tells React to schedule a re-render of the
Countercomponent. - It tells React that the next time
Counterrenders, the value ofcountshould be1.
The trap: Thinking the state variable count changes immediately after calling setCount. It doesn't. State updates are asynchronous. The count variable inside the increment function's scope will always be the value from the render in which that increment function was created.
How to Avoid Stale State
Because state updates are asynchronous and batched, you can't rely on the current state variable if you're calculating the next state from it.
Interviewer prompt: "What happens if you call
setCount(count + 1)three times in the same function? What will the new count be?"The answer is that it will likely only increment by one. Each call to
setCount(count + 1)uses the samecountvalue from the initial render. Ifcountwas 0, all three calls are effectivelysetCount(1).
To solve this, you use the "functional update" form. Instead of passing the new value directly, you pass a function. React will call this function with the guaranteed-latest state and use its return value as the next state.
const increment = () => {
// Good: Use a function to guarantee you get the latest state
setCount(prevCount => prevCount + 1);
};
Always use the functional update form when your new state depends on the previous state. This is a common source of bugs and a clear signal in an interview that you understand React's render cycle.
How React Knows Which State is Which
You may have multiple useState calls in a component. React doesn't use names or IDs to track them. It relies on the call order.
On every single render, your hooks must be called in the exact same order. This is why there are Rules of Hooks:
- Only call Hooks at the top level of your component.
- Don't call Hooks inside loops, conditions, or nested functions.
This stable call order allows React to maintain a list of state cells for your component and associate the first useState call with the first cell, the second with the second, and so on, across renders.
This flow diagram shows the cycle: a state update schedules a re-render, which gets the new state.
Try it
Create a component with a number displayed (defaulting to 0) and two buttons: "+1" and "+5". Ensure that clicking "+1" five times in quick succession results in the same final state as clicking "+5" once. Use the functional update form for both setters to prove it works.
Next: You'll learn how to use state to manage user input and change what's rendered with "Event Handling & Conditional Rendering".
Lock it in — apply this lesson under interview conditions.