Every React component undergoes a continuous lifecycle from its creation in memory to its destruction when the user navigates away. Understanding lifecycle phases is one of the most frequently asked questions in university BCA/CS exams and technical job interviews.
| Phase | Class Lifecycle Methods | Modern Functional Hooks Equivalent |
|---|---|---|
| 1. Mounting (Born into DOM) |
• constructor()• render()• componentDidMount()
|
• Function body executes • Return JSX • useEffect(() => { ... }, [])
|
| 2. Updating (Props/State change) |
• shouldComponentUpdate()• render()• componentDidUpdate()
|
• React.memo (skips render)• useEffect(() => { ... }, [dependencies])
|
| 3. Unmounting (Removed from DOM) |
• componentWillUnmount()
|
• useEffect(() => { return () => cleanup(); }, [])
|
import React, { Component } from 'react';
class LifecycleLogger extends Component {
componentDidMount() {
console.log('1. componentDidMount: DOM is ready; start API calls.');
}
componentDidUpdate(prevProps, prevState) {
console.log('2. componentDidUpdate: Component received new data.');
}
componentWillUnmount() {
console.log('3. componentWillUnmount: Component about to be destroyed.');
}
render() {
return <h3>Component Lifecycle in Action</h3>;
}
}
Now let us solve prop drilling by broadcasting global state in React Context API & useContext.
Accelerate your React mastery by studying these tightly connected core tutorials:
Modern hooks equivalent for class lifecycle methods.
Handling uncaught runtime exceptions in classes.
When lifecycle methods execute during reconciliation.