What React hook is used to run code when a component first appears on the screen?
The useEffect hook with an empty dependency array [] runs code once when the component mounts.
Click to reveal answer
intermediate
How do you clean up resources like timers or subscriptions in React functional components?
Return a cleanup function inside useEffect. This function runs when the component unmounts or before the effect runs again.
Click to reveal answer
intermediate
What is a common use case for running code when a component updates?
Use useEffect with specific dependencies to run code only when those values change, like fetching new data when a prop changes.
Click to reveal answer
advanced
Why should you avoid running expensive code on every render in React?
Because it slows down the app. Use useMemo or useCallback to remember results and avoid unnecessary work.
Click to reveal answer
advanced
How can you simulate componentDidMount, componentDidUpdate, and componentWillUnmount in React functional components?
Use useEffect: with empty dependencies for mount, with dependencies for update, and return a cleanup function for unmount.
Click to reveal answer
Which React hook runs after every render by default?
AuseMemo
BuseState
CuseEffect without dependencies
DuseRef
✗ Incorrect
useEffect without a dependency array runs after every render.
How do you run an effect only once when a component mounts?
AuseEffect with empty dependency array []
BuseEffect with dependencies
CuseEffect with no arguments
DuseState
✗ Incorrect
An empty dependency array [] tells useEffect to run only once on mount.
What does the cleanup function inside useEffect do?
ARuns after every render
BRuns before the effect runs again or when component unmounts
CRuns only on mount
DRuns only on update
✗ Incorrect
The cleanup function runs before the next effect or when the component unmounts.
Which hook helps avoid running expensive calculations on every render?
AuseRef
BuseState
CuseEffect
DuseMemo
✗ Incorrect
useMemo memoizes values to avoid recalculating on every render.
To run code when a specific prop changes, you should:
AUse useEffect with that prop in the dependency array
BUse useState
CUse useRef
DUse useEffect with empty dependencies
✗ Incorrect
Including the prop in useEffect's dependency array runs the effect when it changes.
Explain how to use the useEffect hook to handle component mount, update, and unmount in React functional components.
Think about when useEffect runs and how dependencies control it.
You got /3 concepts.
Describe why and how you would clean up resources like timers or subscriptions in React components.
Consider what happens if you don't clean up after effects.
You got /3 concepts.
Practice
(1/5)
1. What is the primary purpose of the useEffect hook in React functional components?
easy
A. To define event handlers
B. To declare state variables
C. To run code after the component renders or updates
D. To style components dynamically
Solution
Step 1: Understand the role of useEffect
The useEffect hook runs side effects like data fetching or timers after rendering.
Step 2: Compare with other options
Declaring state is done with useState, event handlers are functions, and styling is separate.
Final Answer:
To run code after the component renders or updates -> Option C
Quick Check:
useEffect runs side effects after render [OK]
Hint: useEffect runs code after render or update [OK]
Common Mistakes:
Confusing useEffect with useState
Thinking useEffect is for styling
Believing useEffect defines event handlers
2. Which of the following is the correct syntax to run an effect only once when a React component mounts?
easy
A. useEffect(() => { console.log('Mounted'); });
B. useEffect(() => { console.log('Mounted'); }, []);
C. useEffect(() => { console.log('Mounted'); }, [() => {}]);
D. useEffect(() => { console.log('Mounted'); }, [{}]);
Solution
Step 1: Understand dependency array in useEffect
An empty array [] means the effect runs once on mount only.
Step 2: Analyze other options
Omitting dependencies runs effect after every render; arrays with unstable values like functions or objects run on every render since they are recreated.
Final Answer:
useEffect(() => { console.log('Mounted'); }, []); -> Option B
Quick Check:
Empty array runs effect once on mount [OK]
Hint: Empty dependency array means run once on mount [OK]
Common Mistakes:
Omitting dependency array causes repeated runs
Using non-empty arrays runs effect multiple times
Putting recreated functions or objects in dependency array causes re-runs