Which lifecycle methods in class components are replaced by useEffect in functional components?
Understand how React hooks replace traditional class component lifecycle methods.
intermediateAdvanced hooks and patternsreactuseEffectlifecyclehooks
Published: 10/26/2025
Updated: 10/26/2025
Question
Which lifecycle methods in class components are replaced by useEffect in functional components?
Answer
useEffect can replace componentDidMount, componentDidUpdate, and componentWillUnmount.
| Class Lifecycle Method | Equivalent Hook Behavior |
|---|---|
| componentDidMount | useEffect(() => {...}, []) |
| componentDidUpdate | useEffect(() => {...}, [deps]) |
| componentWillUnmount | useEffect(() => { return () => {...}; }, []) |
Example:
import { useEffect, useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('Mounted or Updated');
return () => {
console.log('Cleanup before unmount');
};
}, [count]);
}
✅ Empty dependency → runs once on mount.
✅ With dependency → runs on updates.
✅ Return cleanup → runs on unmount.
Real-World Example
- Fetching data on mount
- Listening to scroll events
- Cleaning up timers
Quick Practice
Build a timer component that starts on mount and clears on unmount.
Summary
useEffect combines the behavior of componentDidMount, componentDidUpdate, and componentWillUnmount.
Related Videos
Watch these videos to learn more about this topic
Frequently Asked Questions
Can one useEffect replace all lifecycles?
Yes, depending on dependency arrays — it can mimic mount, update, and unmount.
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.