React Component Lifecycle
⏱ Estimated reading time: 2 min
The React component lifecycle describes the different stages a component goes through from creation to removal. Understanding this helps you manage data fetching, side effects, and cleanup effectively.
Lifecycle Phases
React components go through three main phases:
-
Mounting – Component is created and added to the DOM
-
Updating – Component re-renders due to state or prop changes
-
Unmounting – Component is removed from the DOM
Lifecycle in Class Components (Traditional)
Mounting
-
constructor() -
render() -
componentDidMount()
Updating
-
render() -
componentDidUpdate(prevProps, prevState)
Unmounting
-
componentWillUnmount()
Lifecycle in Functional Components (Modern)
Functional components use Hooks instead of lifecycle methods.
useEffect as Lifecycle Replacement
| Lifecycle Phase | Hook Equivalent |
|---|---|
| Mounting | useEffect(() => {}, []) |
| Updating | useEffect(() => {}, [deps]) |
| Unmounting | Cleanup function |
Mounting Example
Updating Example
Unmounting Example (Cleanup)
Complete Lifecycle Example
When Does Re-render Happen?
-
State changes (
useState,useReducer) -
Props change
-
Parent component re-renders
Common Use Cases
-
Fetching data on mount
-
Updating UI when props/state change
-
Cleaning up timers and listeners
-
Managing subscriptions
Best Practices
-
Keep effects focused
-
Clean up side effects
-
Avoid unnecessary re-renders
-
Use dependency arrays correctly
Key Points
-
Lifecycle defines component behavior over time
-
Hooks replace lifecycle methods
-
useEffecthandles side effects -
Cleanup is critical
Register Now
Share this Post
← Back to Tutorials