# `react-doctor/rerender-lazy-ref-init`

Lazy-init the ref: `const ref = useRef(null); if (ref.current === null) ref.current = expensiveCall()`

- **Status:** Active
- **Category:** Performance
- **Assessment:** Evidence-required risk
- **Required evidence:** source code, runtime behavior, measurement
- **Default configuration:** Enabled
- **Default severity:** warn
- **Scope:** All supported frameworks
- **Active when:** always
- **Requirements:** react
- **Tags:** test-noise
- **Priority:** 38 (P3)
- **Source:** oxlint-plugin-react-doctor
- **Rule set:** oxlint-plugin-react-doctor 0.9.3 (prompt schema 2)
- **Documentation:** [Official documentation](https://react.dev/reference/react/useRef)

## Validation prompt

Confirm the detector match and collect the required evidence before deciding whether an edit is warranted.

Fires on a useRef(...) call (isHookCall checks the callee is named useRef) whose FIRST argument is a CallExpression (e.g. useRef(buildExpensiveCache()), useRef(cache.build())) or a NewExpression (e.g. useRef(new AbortController()), useRef(new Map()), useRef(new Set())): useRef has no lazy-initializer form, so that allocation runs every render and is discarded after the first. The callee name is the Identifier name, the member property name for x.y() calls, else "fn". Excluded and NOT flagged: useRef with no args, useRef(null/0/""/initial) (literal or bare identifier: only call/new shapes count), trivial wrappers in TRIVIAL_INITIALIZER_NAMES (Boolean, String, Number, Array, Object, parseInt, parseFloat: so useRef(Number("0")), useRef(String(value)), useRef(Array()) are exempt), and plain-call callees whose name is a React hook (useRef(useId()), useRef(useContext(ThemeContext)): these are already stable and a conditional lazy-init would call a hook conditionally). False positive a reviewer would suppress: a cheap or pure-constant call that costs effectively nothing per render, or an initializer whose freshness-per-render is intentional, where the lazy-ref ceremony adds noise without measurable benefit.

## Evidence boundary

The diagnostic proves only that the detector's modeled source pattern matched. It does not prove runtime impact, product intent, rendered failure, or that one remediation is correct.

Establish the environment, explicit repository policy, relevant exceptions, and required rendered or runtime evidence before deciding the occurrence. This page’s Assessment and Required evidence fields define the review contract.

Default severity is registry metadata. Use the occurrence's JSON severity after repository configuration when ordering real findings.

Record one outcome:

- **Confirmed failure:** The required evidence establishes the violation.
- **Rejected:** A documented exception or false-positive predicate applies.
- **Needs evidence:** Named evidence can still be collected.
- **Unavailable:** Required evidence cannot be collected in this run.
- **Waived with evidence:** An authorized, scoped exception applies to an established failure.
- **Observation:** The review records an optional tradeoff without claiming a defect.

A waiver records its scope, authority, evidence, and review or expiry condition. It is not a pass or false positive.

## Fix prompt

Apply this candidate correction only after the required evidence confirms the risk.

Stop passing the call directly to useRef; initialize the ref lazily so the allocation happens once: const ref = useRef(null); if (ref.current === null) ref.current = buildExpensiveCache(); (for the new case write ref.current = new AbortController();). If the value can safely be recomputed on remount, use const value = useMemo(() => buildExpensiveCache(), []) instead. Keep the trivial/hook-capturing forms unchanged. See https://react.dev/reference/react/useRef

## Repository-wide copy prompt

Use this only for a repository-wide pass after validating each occurrence. For one occurrence, use the occurrence-level guidance above.

````text
Fix every confirmed `react-doctor/rerender-lazy-ref-init` diagnostic in the current repository.

Required change:
- Stop passing the call directly to useRef; initialize the ref lazily so the allocation happens once: const ref = useRef(null); if (ref.current === null) ref.current = buildExpensiveCache(); (for the new case write ref.current = new AbortController();). If the value can safely be recomputed on remount, use const value = useMemo(() => buildExpensiveCache(), []) instead. Keep the trivial/hook-capturing forms unchanged. See https://react.dev/reference/react/useRef.

Validation before editing:
Fires on a useRef(...) call (isHookCall checks the callee is named useRef) whose FIRST argument is a CallExpression (e.g. useRef(buildExpensiveCache()), useRef(cache.build())) or a NewExpression (e.g. useRef(new AbortController()), useRef(new Map()), useRef(new Set())): useRef has no lazy-initializer form, so that allocation runs every render and is discarded after the first. The callee name is the Identifier name, the member property name for x.y() calls, else "fn". Excluded and NOT flagged: useRef with no args, useRef(null/0/""/initial) (literal or bare identifier: only call/new shapes count), trivial wrappers in TRIVIAL_INITIALIZER_NAMES (Boolean, String, Number, Array, Object, parseInt, parseFloat: so useRef(Number("0")), useRef(String(value)), useRef(Array()) are exempt), and plain-call callees whose name is a React hook (useRef(useId()), useRef(useContext(ThemeContext)): these are already stable and a conditional lazy-init would call a hook conditionally). False positive a reviewer would suppress: a cheap or pure-constant call that costs effectively nothing per render, or an initializer whose freshness-per-render is intentional, where the lazy-ref ceremony adds noise without measurable benefit.

Constraints:
- Make the smallest change that fixes the root cause.
- Preserve behavior and interfaces unrelated to this diagnostic.
- Reuse existing project components, utilities, and conventions.
- Do not introduce render-phase side effects, render-phase state updates, or Hooks rule violations.
- Preserve resource ownership and cleanup. Do not move work into a hotter render or frame path.
- If syntax proves only a candidate cost, require a relevant trace, bundle report, payload measurement, or metric before editing.
- Without evidence, record Needs evidence. Compare the same scenario before and after; keep the change only when its target metric improves without regression.
- Adapt identifiers and framework details instead of copying blindly.
- Do not disable the rule or suppress matching code.

Assessment:
- Record detector evidence, applicability facts, assumptions, missing evidence, and the rule class for this occurrence.
- Return one outcome: Confirmed failure, Rejected, Needs evidence, Unavailable, Waived with evidence, or Observation.
- A waiver records the established failure, scope, authority, evidence, and review or expiry condition. It is not a pass or false positive.

Verification:
- Run focused tests for the changed behavior.
- Run React Doctor and confirm this diagnostic no longer appears from changed code.
- Run an unfiltered scan of the affected scope before claiming no cross-category regression.
- Report the files changed and any checks you could not run.
````
