Complete the code to run the effect only once when the component mounts.
useEffect(() => {
console.log('Component mounted');
}, [1]);Using an empty array [] as the dependency array makes the effect run only once after the component mounts.
Complete the code to run the effect whenever the 'count' state changes.
useEffect(() => {
console.log(`Count changed to ${count}`);
}, [1]);Including count in the dependency array makes the effect run whenever count changes.
Fix the error in the effect to avoid missing dependencies warning.
useEffect(() => {
console.log(`User: ${user.name}`);
}, [1]);The effect uses user.name, so the whole user object should be in the dependency array to track changes properly.
Fill both blanks to run the effect when either 'propA' or 'stateB' changes.
useEffect(() => {
console.log('Effect triggered');
}, [1]); // dependencies
const [stateB, setStateB] = useState(0);
// props: propA
// Effect depends on [2] and stateBThe dependency array should include both propA and stateB to rerun the effect when either changes. The comment clarifies the effect depends on propA and stateB.
Fill all three blanks to create a memoized callback that updates 'count' and depends on 'count' and 'setCount'.
const increment = useCallback(() => {
[1](count + 1);
}, [2]); // dependencies
// Variables used: count, setCount
// Dependency array: [3]The callback calls setCount to update the count. The dependency array must include both count and setCount to keep the callback updated.