React Component Lifecycle Explained
Short answer: every React component goes through three phases: mounting (added to the page), updating (re-rendered when props or state change) and unmounting (removed). In modern function components you hook into these phases with useEffect.
The three phases
| Phase | When | Typical work |
|---|---|---|
| Mounting | The component first appears | Fetch data, subscribe, start timers |
| Updating | Props or state change | React to a new value, re-fetch |
| Unmounting | The component is removed | Clean up listeners, timers, subscriptions |
Lifecycle in function components
import { useEffect, useState } from 'react';
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
// Runs after mount, and again whenever userId changes
useEffect(() => {
const controller = new AbortController();
fetch('/api/users/' + userId, { signal: controller.signal })
.then((r) => r.json())
.then(setUser)
.catch(() => {});
// Cleanup runs before the next effect and on unmount
return () => controller.abort();
}, [userId]);
return <p>{user ? user.name : 'Loading...'}</p>;
}
How the dependency array controls timing
| Code | Runs |
|---|---|
useEffect(fn, []) | Once after the first render (mount); cleanup on unmount |
useEffect(fn, [a, b]) | After mount and whenever a or b changes |
useEffect(fn) | After every render |
Learn both hooks in depth in useState and useEffect.
Class component lifecycle methods and their hook equivalents
| Class method | Purpose | Hook version |
|---|---|---|
constructor | Set initial state | useState(initial) |
render | Return the UI | The function body |
componentDidMount | Runs after the first render | useEffect(fn, []) |
componentDidUpdate | Runs after updates | useEffect(fn, [deps]) |
componentWillUnmount | Cleanup | Return a function from useEffect |
shouldComponentUpdate | Skip re-renders | React.memo |
class Clock extends React.Component {
state = { time: new Date() };
componentDidMount() {
this.timer = setInterval(() => this.setState({ time: new Date() }), 1000);
}
componentWillUnmount() {
clearInterval(this.timer);
}
render() {
return <p>{this.state.time.toLocaleTimeString()}</p>;
}
}
Class components still work, but new code uses function components and hooks.
Render vs commit
- Render: React calls your component to work out what the UI should look like. This must be pure, with no side effects.
- Commit: React updates the DOM.
- Effects:
useEffectruns after the browser paints.
Use useLayoutEffect only when you must measure or change the layout before paint.
Strict Mode in development
In development, React Strict Mode mounts, unmounts and mounts a component again to expose missing cleanup. If your effect breaks when it runs twice, add a proper cleanup function. This extra run does not happen in production.
Common mistakes
- Forgetting cleanup for timers, event listeners and subscriptions, causing leaks.
- Leaving out dependencies, which leads to stale values.
- Updating state after unmount from an unfinished request; abort it as shown above.
- Using effects to compute values that can be calculated during rendering.
Frequently asked questions
Why does my useEffect run twice in development?
In React Strict Mode, React deliberately mounts, unmounts and remounts components in development to help find missing cleanup. This does not happen in production builds.
What is the equivalent of componentWillUnmount in hooks?
Return a cleanup function from useEffect. React runs it when the component unmounts and also before the effect runs again.
Should I still learn class lifecycle methods?
Learn the idea, because you will meet class components in older code. For new code, function components with hooks are the standard approach.
Next steps
Read the hooks guide, speed things up with performance optimization, or start from what React is.
