oxc-project/oxc · error · OxcDiagnostic

React Hook {hook_name} has an unnecessary dependency: {dep_n

Error message

React Hook {hook_name} has an unnecessary dependency: {dep_name}.

What it means

Raised by react/exhaustive-deps when the dependency array includes a value from an outer scope (e.g. a module-level mutable or a parent render's variable not relevant to this hook). Outer-scope values are not valid hook dependencies.

Source

Thrown at crates/oxc_linter/src/rules/react/exhaustive_deps.rs:190

    dep_decl_span: Span,
) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "React hook {hook_name} depends on `{dep_name}`, which changes every render"
    ))
    .with_labels([
        span.primary_label("it will always cause this hook to re-evaluate"),
        dep_decl_span.label(format!("`{dep_name}` is declared here")),
    ])
    .with_help("Try memoizing this variable with `useRef` or `useCallback`.")
    .with_error_code_scope(SCOPE)
}

fn unnecessary_outer_scope_dependency_diagnostic(
    hook_name: &str,
    dep_name: &str,
    span: Span,
) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "React Hook {hook_name} has an unnecessary dependency: {dep_name}."
    ))
    .with_label(span)
    .with_help("Consider removing it from the dependency array. Outer scope values aren't valid dependencies because mutating them doesn't re-render the component.")
    .with_error_code_scope(SCOPE)
}

fn infinite_rerender_call_to_set_state_diagnostic(hook_name: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "React Hook {hook_name} contains a call to setState. Without a list of dependencies, this can lead to an infinite chain of updates."
    ))
    .with_label(span)
    .with_help("Consider adding an empty list of dependencies to make it clear which values are intended to be stable.")
    .with_error_code_scope(SCOPE)
}

fn ref_accessed_directly_in_effect_cleanup_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("The ref's value `.current` is accessed directly in the effect cleanup function.")

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the outer-scope value from the dependency array.
  2. Pass the value down as a prop or parameter if it genuinely needs to be reactive.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/react/exhaustive_deps.rs:190 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/419a21df05f0ecd4. Report an issue: GitHub.