EasyReact Fundamentals📖 Theory Question

What is the component lifecycle and how do hooks map to it?

💡

Hint

Mount → Update → Unmount — useEffect covers all three phases

Full Answer

Class components had explicit lifecycle methods. Hooks cover the same phases:

Class methodHook equivalent
componentDidMountuseEffect(() => { ... }, [])
componentDidUpdateuseEffect(() => { ... }, [deps])
componentWillUnmountuseEffect(() => { return () => cleanup() }, [])
shouldComponentUpdateReact.memo + useMemo
getDerivedStateFromPropsCompute during render (no hook needed)
getSnapshotBeforeUpdateuseLayoutEffect return value (rare)
useEffect(() => {
  // componentDidMount — runs after first render
  const sub = subscribe(userId);

  // componentWillUnmount — cleanup function
  return () => sub.unsubscribe();
}, []); // [] = run once

useEffect(() => {
  // componentDidUpdate for userId — runs when userId changes
  fetchUser(userId);
}, [userId]);
💡 useEffect runs AFTER the browser paints. For DOM measurements that need to run synchronously before paint (to avoid flicker), use useLayoutEffect instead.

More React Fundamentals Questions

EasyWhat is React and what problem does it solve?EasyWhat is JSX and how does it work under the hood?EasyWhat is the Virtual DOM and how does reconciliation work?EasyWhat is the difference between a controlled and uncontrolled component?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint