Course contentsShow
← React
Lesson 1022 of 2,39626. Rules of Hooks Deep DiveFree lesson

When to Disable exhaustive-deps (Rarely)

The rare legitimate cases where disabling the rule is correct, like event handlers stored in refs.

When to Disable exhaustive-deps (Rarely)

You'll learn the rare legitimate cases where it's safe to disable the exhaustive-deps linting rule.

The Rule Exists for Good Reason

The exhaustive-deps rule protects you from stale closures and unexpected behavior by ensuring your dependency arrays match what your effect actually uses. Breaking this rule usually leads to bugs. However, there are a few genuinely valid exceptions where you know you're safe and the linter is being overly strict.

The Ref Exception: Event Handlers

The most common legitimate case involves event handlers stored in refs. When you use useRef to hold a function that you update on every render, you don't need to list that ref in your dependencies:

const handlerRef = useRef();
handlerRef.current = myFunction;

useEffect(() => {
  const handler = () => handlerRef.current();
  window.addEventListener('resize', handler);
  return () => window.removeEventListener('resize', handler);
  // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); // Safe: ref.current always points to latest value

The ref itself never changes (it's the same object), so including it as a dependency is pointless. The .current property updates, but React doesn't track that.

Other Rare Cases

  • Intentional one-time effects where you genuinely want to ignore updates to certain values
  • Third-party library callbacks that you know are stable despite what the linter thinks

The Test

Before disabling, ask: "Am I absolutely certain this won't cause a stale closure bug?" If there's any doubt, fix the dependency array instead.

Key Takeaway: Disable exhaustive-deps only when you're using the ref pattern for stable callbacks or have verified that ignoring a dependency is intentional and safe—which is rare. When you do disable it, add a comment explaining why.