Name the main lifecycle phases and methods of a class component. How do you get the same behaviour with hooks in a function component?
Show the explanation
Model answer. A component goes through three phases:
- Mounting (first appears):
constructor→render→componentDidMount. - Updating (props or state change):
render→componentDidUpdate(prevProps, prevState).shouldComponentUpdatecan skip a render. - Unmounting (removed):
componentWillUnmount.
Plus error handling: componentDidCatch / static getDerivedStateFromError (error boundaries).
Hook equivalents:
useEffect(() => { /* componentDidMount */ return () => { /* componentWillUnmount */ }; }, []);
useEffect(() => { /* componentDidUpdate for userId */ }, [userId]);
const Fast = React.memo(Slow); // roughly shouldComponentUpdateThe mental model shifts, though. Class lifecycles are about moments in time ("when mounted, do X"). Effects are about synchronisation ("keep this subscription in sync with userId"). One effect with a cleanup replaces logic that used to be split across componentDidMount, componentDidUpdate and componentWillUnmount, and it is harder to forget the update case.
Error boundaries still need a class component (or a small library such as react-error-boundary).
Original UniCircle question. Last reviewed 22 Oct 2026.
