Course contentsShow
← React
Lesson 549 of 2,39615. useEffect FundamentalsFree lesson

When to Disable the ESLint Rule

Understand the rare cases where disabling exhaustive-deps is justified, and how to do it safely with comments.

When to Disable the ESLint Rule

What you'll learn: You'll discover the rare situations where it's acceptable to silence the exhaustive-deps ESLint warning, and how to do it responsibly.

The General Rule: Don't Disable It

The exhaustive-deps rule exists to protect you from bugs. In 99% of cases, if ESLint is complaining about missing dependencies, you should fix the problem by adding them or refactoring your code. Disabling the rule is like ignoring your car's check engine light—it might feel convenient, but you're likely hiding a real issue.

The Rare Exceptions

There are legitimate (but uncommon) cases where you genuinely know better than the linter:

  1. Intentional stale closures: You specifically want to capture a value from mount time and never update it, even though technically you're using it.

  2. External systems with their own lifecycle: When integrating with third-party libraries that manage their own state independently of React.

  3. Infinite loop prevention: Sometimes adding a dependency creates an unavoidable infinite loop due to object identity issues you can't resolve with useCallback or useMemo.

How to Disable Safely

Always add a comment explaining why:

useEffect(() => {
  // We want mountTimeValue to remain stable forever,
  // ignoring future prop changes intentionally
  console.log(mountTimeValue);
  // eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

The comment forces you (and future readers) to justify the decision. If you can't write a clear explanation, you probably shouldn't disable the rule.

The Better Alternative

Before disabling, triple-check: Can you refactor? Can you move the effect logic to an event handler? Can you use useCallback? These approaches are almost always superior to silencing the warning.

Key Takeaway: Disabling exhaustive-deps should be a last resort with a documented reason—assume the linter is right unless you have clear, defensible evidence otherwise.