In legacy React class components, developers organized code around explicit lifecycle methods:
componentDidMount(): Executed once after the component mounted to the real DOM.componentDidUpdate(prevProps, prevState): Executed after every re-render caused by props or state changes.componentWillUnmount(): Executed immediately before the component was destroyed from the DOM.While intuitive at first, class lifecycles forced unrelated logic to be scattered across multiple methods. For example, setting up a timer in componentDidMount and tearing it down in componentWillUnmount forced related code into separate places.
In modern functional React, Hooks think in terms of synchronization, not lifecycles. However, understanding how useEffect maps to these classic milestones is crucial for mastering React architecture.
useEffectcomponentDidMount: Empty Dependency Array []componentDidUpdate: Specific Dependencies [prop, state]componentWillUnmount: The Return Cleanup FunctionIn class components, componentDidUpdate did not run on initial mount. In contrast, useEffect always runs on initial mount.
If you specifically require an effect to execute only on subsequent updates (skipping the first render), you can use a useRef flag to track whether the component has mounted:
| Milestone | Class Component Method | Functional Component Equivalent |
|---|---|---|
| Initial Mount | componentDidMount() | useEffect(fn, []) |
| Every Render | componentDidUpdate() | useEffect(fn) (no deps array) |
| Specific Change | componentDidUpdate(prevProps) + if | useEffect(fn, [value]) |
| Teardown / Unmount | componentWillUnmount() | useEffect(() => () => cleanup(), []) |
componentDidMount()?componentDidMount and componentWillUnmount problematic?[id] execute?useEffect(fn) (without any dependency array) run on the initial mount?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Removing Unnecessary useEffect Hooks | Controlled vs Uncontrolled Components with useRef |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.