useState Updater
Predict Output Easy
Question
What does this code print?
Code
function App() {
const [c, setC] = useState(0);
setC(prev => prev + 1);
setC(prev => prev + 1);
return <p>{c}</p>;
}
// After batch
Hint
Functional updates chain correctly.
Explanation
Using the functional updater form (prev => prev + 1), each update is based on the previous state. Two increments result in 2. This is the safe way to update state based on previous values.
Related Resources
Related Lessons
- State
Learn state.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.