useState — The Core of Everything — Concept Check
Questions on useState — The Core of Everything. State is a snapshot for a given render, not a variable you read live. That one idea explains stale values, batching, and why you update with a function.
Questions in this quiz
8 questions on useState — The Core of Everything — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a React component, you call setCount(count + 1) twice in a row. What will the final value of count be after these calls?
- AIt will increment by 2.
- BIt will depend on the initial value.
- CIt will stay the same.
- DIt will increment by 1.
2.You have a component that uses useState to store a list of items. What happens if you use push to add an item to the list and then call setRows(rows)?
- AThe list will be duplicated.
- BThe component will re-render with the new item.
- CThe list will be empty.
- DThe component will not re-render.
3.A developer initializes state with useState(() => expensiveComputation()). Why might they choose this method?
- ATo ensure the value updates with props.
- BTo guarantee synchronous execution.
- CTo recompute the value on every render.
- DTo avoid recomputing on every render.
4.After calling setCount(count + 1), you immediately log count. What value will be logged?
- AThe initial count value.
- BUndefined.
- CThe count value plus one.
- DThe updated count value.
5.What is the correct way to update a nested object in state to ensure React re-renders?
- AUse setState with the same object reference.
- BDirectly assign new values to state properties.
- CMutate the object directly and call setState.
- DUse setState with a new object copy.
6.A developer notices their component doesn't see updated props. They initialized state with useState(props.value). Why might this be happening?
- AState initialized with props doesn't update.
- BProps are not passed correctly.
- CuseState doesn't work with props.
- DState updates automatically with props.
7.In React 18+, what happens when multiple state updates occur within the same event handler?
- AOnly the last update is applied.
- BEach update causes a separate re-render.
- CAll updates are batched into one re-render.
- DOnly the first update is applied.
8.Why might a developer use the functional updater form setCount(c => c + 1) instead of setCount(count + 1)?
- ATo avoid using hooks.
- BTo prevent stale closures.
- CTo ensure synchronous updates.
- DTo directly mutate state.