Awesome Reviewers

Reviewer · React

Accurate hook dependencies

When writing or reviewing `useMemo`/`useCallback`/`useEffect` dependency arrays, list every value that is actually read inside the callback, including nested and optional-chained property paths (e.g. `x?.y.z?.a`), and never omit or substitute a less-specific path. Missing a dependency can cause stale values; adding an unnecessarily broad or redundant...

Instruction

When writing or reviewing useMemo/useCallback/useEffect dependency arrays, list every value that is actually read inside the callback, including nested and optional-chained property paths (e.g. x?.y.z?.a), and never omit or substitute a less-specific path. Missing a dependency can cause stale values; adding an unnecessarily broad or redundant dependency (e.g. both x and x.y) causes extra re-computation, so prefer the most precise path actually accessed.

Do not assume a value is referentially stable just because it looks like a ref or comes from a custom hook (e.g. useCustomRef()). Unless the hook’s contract guarantees identity stability, treat its return value as a normal dependency and include it in the dependency array — the hook could internally swap between multiple refs/state values across renders, so skipping it as a dependency can lead to subtle bugs.

Example of correct, exhaustive dependencies:

function useCustomRef() {
  // may return different ref instances across renders
  return condition ? refA : refB;
}

function Foo() {
  const customRef = useCustomRef();
  // customRef must be listed since its identity isn't guaranteed stable
  const callback = useCallback(() => {
    customRef.current?.focus();
  }, [customRef]);
}

function Component({x, y, z}) {
  // use the exact accessed path as the dependency
  const a = useMemo(() => x?.y.z?.a, [x?.y.z?.a]);
}

Enforce this with exhaustive-deps lint rules (or compiler validation) and treat any missing or over-broad dependency as a bug to fix rather than suppress.