Full answer
Class components are legacy for new work, yet many codebases have them and interviewers ask about the lifecycle to check you understand what hooks replaced.
Mounting: the constructor sets initial state and binds methods. render returns JSX and must be pure. componentDidMount runs once after the first commit, which is the place to start subscriptions, timers or requests.
Updating happens when props or state change. shouldComponentUpdate(nextProps, nextState) may return false to skip rendering (PureComponent does a shallow comparison for you). render runs again. getSnapshotBeforeUpdate reads the DOM just before it changes, for example a scroll position. componentDidUpdate(prevProps, prevState) runs after the commit. Compare with previous values before doing work, otherwise you get an infinite loop when you call setState there.
Unmounting: componentWillUnmount runs once before removal. Clean up what componentDidMount started.
Errors: static getDerivedStateFromError returns state to show a fallback, and componentDidCatch logs.
Names that were renamed or removed: componentWillMount, componentWillReceiveProps and componentWillUpdate carry the UNSAFE_ prefix because they misbehave with concurrent rendering. Do not use them.
setState is asynchronous and batched. When the next state depends on the previous, pass a function: this.setState((s) => ({ count: s.count + 1 })). Event handlers must be arrow functions or bound, otherwise this is undefined.
The mapping to hooks is useEffect for mount, update and cleanup, useState for state, and useMemo or memo for shouldComponentUpdate. The logs in the example show the order.
import { Component } from 'react';
class Timer extends Component {
state = { seconds: 0 };
componentDidMount() {
console.log('mounted with step ' + this.props.step);
}
componentDidUpdate(prevProps, prevState) {
if (prevState.seconds !== this.state.seconds) {
console.log('updated: ' + prevState.seconds + ' to ' + this.state.seconds);
}
}
componentWillUnmount() {
console.log('unmounting at ' + this.state.seconds);
}
tick = () => {
this.setState((s) => ({ seconds: s.seconds + this.props.step }));
};
render() {
return <button onClick={this.tick}>Seconds: {this.state.seconds}</button>;
}
}
export default class App extends Component {
state = { show: true };
render() {
return (
<div>
<button onClick={() => this.setState({ show: false })}>Remove timer</button>
{this.state.show && <Timer step={5} />}
</div>
);
}
}The check pressed the remove button first, which unmounts the timer, so only the mount and unmount lines appear. Output: mounted with step 5 updated: 0 to 5 unmounting at 5
Key points
- Mount: constructor, render, componentDidMount. Update: render, componentDidUpdate. Unmount: componentWillUnmount.
- Compare previous and current values in componentDidUpdate before calling setState.
- Use the functional form of setState when the next state depends on the previous.
- UNSAFE_ lifecycle methods should not be used.
- Hooks replaced this with useEffect, useState and memo. Error boundaries still require a class.
Common mistakes
- Calling setState in componentDidUpdate without a condition, which loops forever.
- Mutating this.state directly.
- Forgetting to bind a normal method, so this is undefined.
- Starting a subscription in componentDidMount and not removing it in componentWillUnmount.
- Using the UNSAFE_ methods in new code.
Follow-ups
- How does getDerivedStateFromProps differ from componentDidUpdate, and why is it rarely needed?
- How would you convert this component to a function with hooks?
- What does PureComponent do?
Tips
- Say it is legacy, then show you know it well. That is the balanced answer.
- Give the order of calls for mount and update.
- Mention the hook equivalents at the end.