DevAcademy
LearnReactComponent Lifecycle
IntermediateReact

Component Lifecycle

Understand the three phases every component goes through — mounting, updating, and unmounting — and how hooks map onto them.

Reading Time

14 min

Lesson

Lesson 14 of 42

The Three Lifecycle Phases

Every component goes through the same three phases: mounting (created and inserted into the DOM for the first time), updating (re-rendered due to new props or state), and unmounting (removed from the DOM).

Lifecycle Phases

PhaseWhen It Happens
MountingThe component is rendered for the first time
UpdatingThe component re-renders due to new props, state, or context
UnmountingThe component is removed from the DOM entirely

How useEffect Maps to the Lifecycle

Rather than separate lifecycle methods (as in older class components), useEffect covers all three phases through its dependency array and cleanup function.

Simulating Lifecycle Phases with useEffect

useEffect(() => {
  console.log("Mounted, or a dependency changed");

  return () => {
    console.log("Cleaning up before the next run, or before unmounting");
  };
}, [dependency]);

Mapping useEffect to Lifecycle Events

Class Component MethoduseEffect Equivalent
componentDidMountuseEffect(() => {...}, [])
componentDidUpdateuseEffect(() => {...}, [dep])
componentWillUnmountThe cleanup function returned from useEffect

Class Components Had Explicit Lifecycle Methods

Before hooks, class components used named methods for each phase — componentDidMount, componentDidUpdate, componentWillUnmount — spreading related logic (like subscribing and unsubscribing) across different methods instead of together in one place.

The Old Class Component Approach (For Reference)

class Timer extends React.Component {
  componentDidMount() {
    this.id = setInterval(() => this.tick(), 1000);
  }

  componentWillUnmount() {
    clearInterval(this.id);
  }

  tick() {
    // ...
  }

  render() {
    return <p>Timer running</p>;
  }
}

Best Practice

Think in terms of "synchronizing with a dependency" rather than "running code at a lifecycle moment" — it’s a more accurate mental model for how useEffect actually behaves, and helps avoid common effect-related bugs.

Interview Questions

Quick Quiz

1. What are the three phases of a component’s lifecycle?

2. Which useEffect pattern corresponds to componentDidMount?

3. What is an advantage of useEffect over separate class lifecycle methods?