The component lifecycle, thought about through hooks
Why mount/update/unmount thinking from class components is the wrong mental model for hooks, and how to think in terms of synchronization instead.
The component lifecycle, thought about through hooks
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Class components exposed lifecycle methods — componentDidMount, componentDidUpdate, componentWillUnmount — that named specific moments in a component's life, and a lot of real-world code and tutorials still talk in those terms.
- Hooks don't give you a direct replacement for those methods; useEffect is not 'componentDidMount in disguise,' even though a dependency array of [] makes it look that way.
- The right mental model for an effect is synchronization: you're keeping something outside React (a subscription, the DOM, localStorage, a timer) in sync with your component's current props and state, not reacting to a named lifecycle event.
- Treating useEffect as a lifecycle hook is the single most common source of effect bugs, because it leads people to ask 'when does this run' instead of 'what is this syncing, and what does it depend on'.
- A component still conceptually mounts, updates, and unmounts, but hooks don't want you to write code keyed to those three moments; they want you to describe a relationship that stays true across all of them.
- This shift in thinking is a prerequisite for using useEffect correctly, which is why it comes before the hook itself in a hooks-based curriculum.
The lifecycle you probably already know
If you’ve read any React tutorial written before 2019, or worked in a codebase that still has class components, you’ve seen this shape:
class UserProfile extends React.Component {
componentDidMount() {
this.subscription = subscribeToUser(this.props.userId, (user) => {
this.setState({ user });
});
}
componentDidUpdate(prevProps) {
if (prevProps.userId !== this.props.userId) {
this.subscription.unsubscribe();
this.subscription = subscribeToUser(this.props.userId, (user) => {
this.setState({ user });
});
}
}
componentWillUnmount() {
this.subscription.unsubscribe();
}
render() {
return <div>{this.state.user?.name}</div>;
}
}
Three methods, three moments: mount, update, unmount. Each one names a point in the component’s life, and you write code for that specific point. It’s a reasonable model, and a lot of real-world React code, Stack Overflow answers, and older courses still think in these terms, so it’s worth knowing the names even if you never write a class component yourself.
Notice the duplication, though: the subscribe logic appears twice, once in componentDidMount and once (conditionally) in componentDidUpdate, and the cleanup appears in two different methods too. That duplication is a symptom of forcing a single idea — “stay subscribed to the right user” — into three separate lifecycle buckets.
The hook that looks like it replaces them, but doesn’t
Here’s the hooks version of the same component:
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
useEffect(() => {
const subscription = subscribeToUser(userId, (nextUser) => {
setUser(nextUser);
});
return () => subscription.unsubscribe();
}, [userId]);
return <div>{user?.name}</div>;
}
It’s tempting to read this as “the setup function runs on mount, and the returned cleanup runs on unmount” — which would make useEffect just a friendlier componentDidMount plus componentWillUnmount. But that reading breaks the moment userId changes: React doesn’t only call the setup function once. It calls the cleanup from the previous render’s effect, then runs the setup function again, every single time userId changes — not just on mount and unmount. There is no separate “update” branch to write, because there’s no update case to handle: the entire effect body already describes what should be true for any value of userId, and React re-runs it whenever that value changes.
This is the mental shift. The class version asks “what do I do when this component mounts, updates, or unmounts?” The hook version asks “what does the subscription need to look like for the current userId, and how do I tear down the previous one before setting up the new one?” Those are different questions, and the second one is the one useEffect is actually designed to answer.
Synchronization, not events
Think of an effect as a sync job, similar to how a music app keeps your phone’s playlist in sync with the server: whenever the source of truth changes, it tears down the old local copy and builds a new one that matches. It doesn’t matter whether that’s the “first sync” or the “tenth sync” — the logic is identical every time. useEffect’s dependency array is exactly that trigger: “re-run this sync whenever one of these values is different from last time.”
Once you stop asking “is this the mount case or the update case,” effects get much simpler to reason about, and bugs like the duplicated-subscribe-logic above stop happening, because there’s only one code path instead of three.
What the video covers
Theo’s video makes this argument forcefully: useEffect is not a lifecycle hook, and treating it like one — reaching for it to replicate componentDidMount/componentDidUpdate/componentWillUnmount — is, in his words, the most dangerous way to use it. He walks through the kinds of bugs that model produces and argues for thinking about effects as synchronization instead. The video moves fast and assumes you already recognize the three class lifecycle methods by name; this lesson spells those out explicitly first so the contrast lands even if you’ve never written a class component.
Where this fits
With the mental model in place, the next lesson gets concrete about the hook itself: useEffect and synchronizing with external systems covers how the dependency array is actually compared, the bugs that come from missing or extra dependencies, and when you shouldn’t be reaching for an effect at all.
Check yourself
What did componentDidMount, componentDidUpdate, and componentWillUnmount let a class component do?
They let a class component run code at three named moments: right after it first rendered into the DOM, right after a re-render caused by changed props or state, and right before it was removed from the DOM.
Why is it misleading to think of useEffect as 'the hooks version of componentDidMount'?
Because useEffect's real job is synchronizing with something outside React on every render where its dependencies changed, not firing at a named lifecycle moment; a dependency array of [] happens to make it run once, but writing it to mimic componentDidMount leads to effects that silently go stale when the values they use later change.
What question should you ask when writing an effect, instead of 'when does this run'?
What external thing am I keeping in sync, and with which values does it need to stay in sync — because the answer to that question determines the effect's dependency array, not the other way around.
Does a component still mount, update, and unmount when you write it with hooks instead of a class?
Yes, those are events that happen to a component regardless of how it's written. What changes with hooks is that you stop writing code keyed to those named moments and instead describe an ongoing relationship that React re-synchronizes as needed.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.