Derived State
Predict Output Easy
Question
What does this code print?
Code
function App() {
const [items] = useState([1, 2, 3, 4]);
const total = items.reduce((a, b) => a + b, 0);
return <p>{total}</p>;
}
// What renders?
Hint
Derived values are computed during render from state.
Explanation
items is [1,2,3,4]. reduce sums them: 1+2+3+4=10. The total is computed during render (not stored in state). The paragraph shows 10.
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.