In the questions below, consider the following component:
import React, { useState, useEffect } from "react"; const LifecycleDemo = () => { const [count, setCount] = useState(0); useEffect(() => { console.log("Component mounted"); return () => { console.log("Component unmounted"); }; }, []); useEffect(() => { console.log(`Count updated to ${count}`); return () => { console.log(`Cleanup before count changes or unmount: ${count}`); }; }, [count]); return ( <div> <h1>React Lifecycle with useEffect</h1> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); }; export default LifecycleDemo;
You’re building a dashboard that fetches user data when the component first loads. You decide to use useEffect to handle this. Based on the LifecycleDemo code, which useEffect setup would ensure the data fetch happens only once when the component mounts?
useEffect
LifecycleDemo
useEffect(() => { console.log("Mounted"); });
useEffect(() => { fetchData(); }, []);
useEffect(() => { fetchData(); }, [data]);
useEffect(() => { fetchData(); }, [count]);
useEffect(() => { fetchData(); return () => { console.log("Cleanup"); }; }, []);
In the LifecycleDemo component, you click the "Increment" button, changing count from 0 to 1. What will you see in the console based on the second useEffect (the one tracking count)?
count
"Component mounted"
"Count updated to 0"
"Count updated to 1"
"Cleanup before count changes or unmount: 0"
"Cleanup before count changes or unmount: 1"
Imagine you’re adding a feature to LifecycleDemo to log "Reset" when the component unmounts. Which of these modifications to the first useEffect (empty dependency array) would achieve this?
Change the dependency array to [count] and log "Reset"
[count]
Add console.log("Reset"); inside the effect callback
console.log("Reset");
Place console.log("Reset"); outside the useEffect
Add a second useEffect(() => { console.log("Reset"); }, []);
useEffect(() => { console.log("Reset"); }, []);
Add return () => { console.log("Reset"); }; inside the useEffect
return () => { console.log("Reset"); };
A student modifies LifecycleDemo by adding a useEffect to track a new state variable user like this:
user
const [user, setUser] = useState("Guest"); useEffect(() => { console.log(`User is: ${user}`); }, [count]);
If count changes from 1 to 2 but user stays "Guest," what happens?
"User is: 2" logs incorrectly
Nothing logs because user didn’t change
"Component mounted" logs instead
"User is: Guest" logs because count changed
The cleanup function runs first
You’re debugging LifecycleDemo and notice that after clicking "Increment" three times (count goes 0 → 1 → 2 → 3), the console shows multiple cleanup messages. Why does the second useEffect (with [count]) run its cleanup function repeatedly?
React is re-rendering unnecessarily
The cleanup runs before the effect re-runs when count updates
The empty dependency array in the first useEffect causes it
The component is unmounting each time count changes
The cleanup only runs on unmount, so this shouldn’t happen
This content is only available to registered users.Please register at the the home page.