React · Junior · Question 12 of 75
What does console.log print after a click?
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
console.log(count);
}- 1
1 - ✓Correct answer:
0 - 3
undefined
Explanation
State within a render is a "snapshot". setCount schedules a new render, but the count variable in the current function stays the same. The new value is available on the next render. If you need the value right away, compute it into a variable: const next = count + 1.