useState: Giving Components Memory

useState gives a component its own memory, letting it track information that changes over time. It's used for everything from handling form input to toggling UI. The biggest footgun: state updates are asynchronous and only apply on the next render.
Why it exists
Function components are stateless by nature; they execute and their variables disappear. To build interactive UIs, components need a way to remember information between renders, like what a user has typed into an input field. The useState Hook is React's fundamental solution for adding this memory, or "state," to components.
The mental model
Think of useState as asking React to remember a value for you. You give it an initial value, and it gives you back a pair: the current value and a special function to update that value. Calling this "setter" function doesn't just change a variable; it tells React, "this important value has changed, so please re-render this component and any children with the new information."
How it works
You call useState at the top level of your component, passing an initial value. It returns an array with exactly two items: const [age, setAge] = useState(28);. The first item is the current state value, and the second is the setter function. When you call setAge(29), React schedules a re-render. On the next render, the useState call will return 29 as the current value for age. If calculating the initial state is expensive, you can pass a function, useState(() => createTodos()), which React will only run on the very first render.
When to use it
Use useState for any data that changes over time and should cause a UI update. This is your primary tool for handling user interaction. Examples include managing form inputs, tracking the open/closed state of a modal, storing data fetched from an API, or keeping score in a game. If a value changes and the user should see that change, it probably belongs in state.
When not to use it
Avoid state for values that can be calculated from existing props or other state variables during render. Also, don't call Hooks like useState inside loops, conditions, or nested functions. If you need conditional state, extract the logic into a new component. For values that need to persist across renders but don't trigger UI updates (like a timer ID), use the useRef Hook instead.
One canonical example
A simple counter shows the key footgun. You might initialize state with const [count, setCount] = useState(0);. To increment, you call setCount(count + 1). The mistake is assuming count will be 1 on the very next line of code. It won't. State updates are queued. The count variable in the current render's scope is still 0. The new value is only available when the component re-renders.
Interview question
What is the immediate effect when a useState setter function, like setCount(count + 1), is invoked?
- a.The count variable in the current component scope updates immediately.
- b.The component's props are updated, triggering a re-render.
- c.React schedules a re-render of the component with the new state value.Correct
- d.The component synchronously re-executes its function body.
Why? this is the answer
Calling a useState setter function tells React to schedule a re-render with the new state value, as stated by 'React schedules a re-render.' The 'count' variable in the current render's scope does not update immediately; this is a common 'footgun' where the new value is only available on the next render.
Just read this? Test yourself on what you have been reading.
Read the original → react.dev
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.
See open roles