Complete the code to run a function after the component mounts.
useEffect(() => {
console.log('Component mounted');
}, [1]);Using an empty array [] as the dependency list runs the effect only once after the component mounts.
Complete the code to clean up a subscription when the component unmounts.
useEffect(() => {
const id = subscribe();
return () => {
[1](id);
};
}, []);The cleanup function should call unsubscribe to stop the subscription when the component unmounts.
Fix the error in the effect to update the document title when count changes.
useEffect(() => {
document.title = `Count: ${count}`;
}, [1]);The effect should depend on count so it runs whenever count changes.
Fill both blanks to create a state and update it on button click.
const [[1], [2]] = useState(0); <button onClick={() => [2]([1] + 1)}>Increment</button>
The first blank is the state variable name, and the second is the function to update it.
Fill all three blanks to fetch data on mount and store it in state.
const [[1], [2]] = useState(null); useEffect(() => { fetch('/api/data') .then(res => res.json()) .then(data => [2](data)); }, [3]);
We create a state variable data and its setter setData. The effect runs once on mount with an empty dependency array [].