05 / 09

Describe componentDidMount() method.

When called, it examine this.props and this.state and return one of the following types:
  1. 1

    componentDidMount() is invoked immediately after a component is mounted (inserted into the tree).

  2. 2

    Initialization that requires DOM nodes should go here.

  3. 3

    If you need to load data from a remote endpoint, this is a good place to instantiate the network request.

  4. 4

    This method is a good place to set up any subscriptions. If you do that, don’t forget to unsubscribe in componentWillUnmount().

You may call setState() immediately in componentDidMount(). It will trigger an extra rendering, but it will happen before the browser updates the screen.
  1. 1

    This guarantees that even though the render() will be called twice in this case, the user won’t see the intermediate state.

  2. 2

    Use this pattern with caution because it often causes performance issues. In most cases, you should be able to assign the initial state in the constructor() instead.

  3. 3

    It can, however, be necessary for cases like modals and tooltips when you need to measure a DOM node before rendering something that depends on its size or position.

Difficulty: 5/10
Topics: mounting lifecycle, side effects, cleanup

Scenario Questions

0-2 years experience
  1. 1

    You need to fetch a user's profile when a component mounts. How would you use componentDidMount to do that, and what would happen if you moved the fetch to the constructor instead?

  2. 2

    If you place a setTimeout inside componentDidMount, when does it fire relative to the initial render?

  3. 3

    What will the UI show if componentDidMount updates state synchronously right after the first render?

2-5 years experience
  1. 1

    We added a WebSocket subscription in componentDidMount and unsubscribe in componentWillUnmount, but after a recent change the subscription leaks. Walk me through how you'd debug it.

  2. 2

    During a feature rollout we moved data fetching from componentDidMount to getDerivedStateFromProps and started seeing duplicate API calls. Why did that happen?

  3. 3

    Explain the trade‑offs of initiating an async operation in componentDidMount versus using useEffect in a functional component.

5-8 years experience
  1. 1

    Our dashboard renders hundreds of widgets, each starting a timer in componentDidMount, causing performance issues. How would you redesign the lifecycle usage to mitigate the impact?

  2. 2

    We need side‑effects in componentDidMount to run only after certain props are validated, but those props can change later. How would you coordinate this without race conditions?

  3. 3

    When server‑side rendering a component that uses componentDidMount for analytics, how do you prevent errors and still capture data on the client?

8+ years experience
  1. 1

    Our codebase mixes class components with componentDidMount and new hooks. We're planning a migration. How would you decide which components to refactor first, and what patterns would you enforce to avoid lifecycle bugs across teams?

  2. 2

    A cross‑team feature requires consistent initialization logic across dozens of class components. Propose an architectural approach (e.g., HOC, base class) to centralize componentDidMount behavior while keeping testability.

  3. 3

    During a major performance overhaul we discovered heavy calculations in componentDidMount blocking the main thread. How would you redesign the system to offload work, considering existing constraints and future scalability?

Follow-up Questions

  • Can you describe what happens if you call setState inside componentDidMount?
  • How would you verify that a subscription is correctly cleaned up?
  • What differences would you expect if you rewrote this with useEffect?