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.

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint