Complete the code to update state and trigger a re-render in a React functional component.
const [count, setCount] = useState(0); function increment() { setCount([1]); }
Using setCount(count + 1) updates the state by increasing the count by 1, which triggers a re-render.
Complete the code to correctly read the current state value inside a functional component.
const [name, setName] = useState(''); return <p>Hello, [1]!</p>;
The variable name holds the current state value, so it should be used to display the name.
Fix the error in the code to properly update state based on the previous state value.
setCount([1] => [1] + 1);
Using a function with a parameter like prev ensures you get the latest state value when updating.
Fill both blanks to create a state object and update one property without losing others.
const [user, setUser] = useState({ name: '', age: 0 });
setUser({ ...user, [1]: [2] });Using the spread operator ...user keeps existing properties, and updating name to 'John' changes only that property.
Fill all three blanks to create a memoized callback that updates state only when dependencies change.
const increment = useCallback(() => {
setCount([1] + 1);
}, [[2], [3]]);The callback uses the current count to update state, and the dependencies include count and setCount to ensure the function updates correctly.