Lifecycle methods are only available in class components. Functional components use hooks like
useEffect instead.Component lifecycle phases
A component goes through three main phases:- Mounting - Component is created and inserted into the DOM
- Updating - Component re-renders due to prop or state changes
- Unmounting - Component is removed from the DOM
Mounting phase
These methods are called when a component is being created and inserted into the DOM.constructor()
Called before the component is mounted. Used for initializing state and binding methods.componentWillMount()
Called immediately before mounting occurs. It’s invoked during the diff process (src/diff/index.js:168):
componentDidMount()
Called immediately after the component is mounted. Perfect for:- Fetching data from APIs
- Setting up subscriptions
- Adding event listeners
- Interacting with the DOM
src/diff/index.js:173):
Example usage
Updating phase
These methods are called when a component’s props or state change.getDerivedStateFromProps()
Static method called before rendering, both on initial mount and on updates. Returns an object to update state, ornull to update nothing.
Implementation (src/diff/index.js:148):
Example usage
getDerivedStateFromProps is static, so it doesn’t have access to this. It’s a pure function based only on props and state.componentWillReceiveProps()
Called when a component receives new props (src/diff/index.js:177):
shouldComponentUpdate()
Called before rendering when new props or state are received. Returnfalse to skip rendering.
Implementation (src/diff/index.js:186):
Example usage
This is a powerful performance optimization. Use it to prevent unnecessary renders.
componentWillUpdate()
Called before rendering with new props or state (src/diff/index.js:223):
getSnapshotBeforeUpdate()
Called right before DOM updates are committed. Return value is passed tocomponentDidUpdate().
Implementation (src/diff/index.js:270):
Example usage
componentDidUpdate()
Called immediately after updating occurs. Good for:- Operating on the DOM after an update
- Making network requests based on prop changes
src/diff/index.js:227):
Example usage
Unmounting phase
componentWillUnmount()
Called immediately before a component is unmounted and destroyed. Perfect for:- Cleaning up timers
- Canceling network requests
- Removing event listeners
- Cleaning up subscriptions
src/diff/index.js:695):
Example usage
Lifecycle method order
On mount
constructor()getDerivedStateFromProps()render()- DOM updates
componentDidMount()
On update
getDerivedStateFromProps()shouldComponentUpdate()render()getSnapshotBeforeUpdate()- DOM updates
componentDidUpdate()
On unmount
componentWillUnmount()- Component removed from DOM
Complete lifecycle example
Here’s a real-world example demonstrating multiple lifecycle methods:- Initializing state in
constructor() - Deriving state from props with
getDerivedStateFromProps() - Fetching data in
componentDidMount() - Updating when props change in
componentDidUpdate() - Cleaning up in
componentWillUnmount() - Optimizing with
shouldComponentUpdate()
Error boundaries
Error boundaries catch JavaScript errors anywhere in their component tree:Error boundaries only catch errors in child components, not in the error boundary itself.