Consider this React component that updates state using a callback function:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
function increment() {
setCount(prevCount => prevCount + 1);
}
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}If the user clicks the button twice quickly, what will be the displayed count?
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); function increment() { setCount(prevCount => prevCount + 1); } return ( <div> <p>Count: {count}</p> <button onClick={increment}>Increment</button> </div> ); }
Think about how React batches state updates and how the callback function uses the previous state.
Using a callback function with setCount ensures each update uses the latest state value. Clicking twice quickly will increment count twice, resulting in 2.
Choose the correct way to update a React state variable score by adding 5 using a callback function.
Remember the callback receives the previous state as an argument.
Option A correctly uses a callback function that takes prevScore and returns the new value. Others either use the current state directly or have syntax errors.
Look at this React component:
function Clicker() {
const [clicks, setClicks] = React.useState(0);
function handleClick() {
setClicks(clicks + 1);
setClicks(clicks + 1);
}
return (
<button onClick={handleClick}>Clicks: {clicks}</button>
);
}After clicking the button once, what is the value of clicks and why?
function Clicker() { const [clicks, setClicks] = React.useState(0); function handleClick() { setClicks(clicks + 1); setClicks(clicks + 1); } return ( <button onClick={handleClick}>Clicks: {clicks}</button> ); }
Consider how React batches state updates and the value of clicks inside the function.
Both setClicks calls use the same clicks value from closure (stale). So state updates to 1, not 2.
Given this React component:
function MultiIncrement() {
const [count, setCount] = React.useState(0);
function incrementThrice() {
setCount(c => c + 1);
setCount(c => c + 1);
setCount(c => c + 1);
}
return (
<button onClick={incrementThrice}>Count: {count}</button>
);
}What will the count show after clicking the button once?
function MultiIncrement() { const [count, setCount] = React.useState(0); function incrementThrice() { setCount(c => c + 1); setCount(c => c + 1); setCount(c => c + 1); } return ( <button onClick={incrementThrice}>Count: {count}</button> ); }
Each callback receives the updated state from the previous callback.
Using callback functions ensures each update adds 1 to the latest state, so three calls add 3 total.
Which reason best explains why React recommends using callback functions with setState when new state depends on previous state?
Think about how React batches multiple state updates asynchronously.
React batches state updates, so the state variable might be stale inside event handlers. Callback functions get the latest state value to avoid bugs.