ReactInterview preparation
Intermediate·Concept·2 min read

Why does an effect run twice in development or loop forever?

Practice this question

Definition:

React Strict Mode performs an extra development-only setup and cleanup cycle for effects to reveal missing cleanup. An infinite effect loop is a different issue, usually caused by updating state that keeps changing an effect dependency. Understanding the trigger helps fix the design rather than masking the behavior.

Key points:

  • Strict Mode runs an extra setup-and-cleanup cycle in development to expose bugs.
  • A loop often means the effect changes state that changes its own dependencies.
  • Fix the dependency or synchronization design; do not simply remove dependencies.

Reference: React useEffect.

  • An object or function created during every render has a new identity and can retrigger an effect that depends on it.

Interview point:

  • Do not claim production always runs every effect twice, and do not remove required dependencies just to silence a loop.
Adapted from 06_REACT_NEXTJS.md · “Why does an effect run twice in development or loop forever?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.