Skip to content

Clarify dependency inference for built-in hooks inside custom hooks #326

Closed

Description

@aleclarson

Problem

The Compiler-inferred hook dependencies section in docs/differences-from-react.md accurately describes both built-in hook dependency inference and the narrower analysis of transparent custom-hook wrappers, but it does not clearly distinguish those two cases.

In particular, this sentence can be read as saying that a custom hook implemented in plain .ts/.js cannot omit the dependency array from an internal useEffect call:

plain .ts/.js modules, imported custom hooks, method hooks, and wrappers that transform or inspect those parameters require an explicit dependency argument.

That is not the actual limitation. A compiler-processed custom hook in plain TypeScript can call a built-in hook without a dependency array, and the compiler infers the built-in hook's lexical captures:

export function useLogger(value: string) {
  useEffect(() => {
    console.log(value);
  });
}

The repository already covers this behavior in packages/octane/tests/_fixtures/auto-hook-deps-external.ts and packages/octane/tests/auto-hook-deps-behavior.test.ts.

The narrower restriction applies when the compiler is asked to infer and append a dependency argument at a call to a custom dependency-forwarding wrapper. That is supported only for locally declared, transparent wrappers in fully compiled .tsrx/.tsx modules; imported custom hooks, method hooks, and plain-.ts/.js wrapper calls need an explicit argument.

Suggested documentation change

Explicitly separate these cases and include a short example:

  1. A built-in useEffect/useMemo/etc. call inside a compiler-processed custom hook may omit its dependency array in .tsrx, .tsx, .ts, or .js; dependencies are inferred from the built-in hook callback.
  2. An omitted dependency argument at a call to a custom wrapper is inferred only when the wrapper is locally declared and transparently forwards its callback and final dependency parameter in a fully compiled .tsrx/.tsx module.
  3. Imported wrappers, method hooks, non-transparent wrappers, and custom-wrapper calls in the surgical plain-.ts/.js pass require an explicit dependency argument.
  4. Restate the three authored forms: omission means compiler inference, [] is an authoritative empty list, and null means every render/recomputation.

This would make the answer to “Can a custom hook call useEffect() without a dependency array?” unambiguously yes, while preserving the documented static-analysis boundary for custom wrapper calls.

Activity

  1. domgan-oai commented on Jul 28, 2026

    @domgan-oai
    Contributor

    Thanks for the clear report. I checked this against current main (dac0e660).

    Triage: confirmed documentation defect; no compiler/runtime change is required.

    packages/octane/tests/_fixtures/auto-hook-deps-external.ts already defines plain-TypeScript custom hooks that omit the dependency arrays of their internal useEffect and useMemo calls. packages/octane/tests/auto-hook-deps-behavior.test.ts explicitly verifies that those effects and memos track their captures correctly. The problem is that docs/differences-from-react.md conflates inference at a direct built-in hook call with inference at a call to a dependency-forwarding custom wrapper.

    Proposed resolution:

    1. Rewrite the compiler-inferred-dependencies section to separately explain direct built-in-hook inference and the narrower local transparent-wrapper analysis.
    2. Include a plain-.ts custom-hook example showing that an internal useEffect/ useMemo may omit its array.
    3. Explain that imported, method, opaque, and plain-.ts/ .js wrapper calls need an explicit dependency argument; do not incorrectly apply that restriction to the built-in hook calls inside those modules.
    4. Restate the existing authored contract: omission requests inference, an explicit array remains authoritative, and null means every render/recomputation.
    5. Keep the existing plain-TypeScript behavioral regression passing.

    This should be handled as a focused documentation fix rather than changing supported hook semantics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions