Complete the code to create a React state hook for a counter.
const [count, [1]] = useState(0);
The useState hook returns a pair: the current state and a function to update it. The convention is to name the updater function starting with 'set'.
Complete the code to update the state when a button is clicked.
<button onClick={() => [1](count + 1)}>Increment</button>To update the state, you call the updater function returned by useState. Here, setCount updates the count state.
Fix the error in the code to synchronize state with a server response using useEffect.
useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(data => [1](data));
}, []);Inside useEffect, to update the React state with fetched data, you must call the updater function, here setData.
Fill both blanks to create a controlled input that updates state on change.
<input type="text" value=[1] onChange={e => [2](e.target.value)} />
The value attribute should be bound to the state variable (inputValue), and the onChange handler should call the updater function (setInputValue) with the new input.
Fill all three blanks to create a memoized callback that updates state only when needed.
const handleClick = useCallback(() => {
[1]([2] + 1);
}, [[3]]);The useCallback hook memoizes the function. Inside, you call the updater setCount with count + 1. The dependency array includes count to update the callback when count changes.