# `react-doctor/no-create-store-in-render`

Hoist the store/atom/observable construction to module scope: const store = create(...) outside the component.

- **Status:** Active
- **Category:** Correctness
- **Assessment:** Evidence-required risk
- **Required evidence:** source code
- **Default configuration:** Enabled
- **Default severity:** error
- **Scope:** All supported frameworks
- **Active when:** always
- **Requirements:** react
- **Priority:** 72 (P1)
- **Source:** oxlint-plugin-react-doctor
- **Rule set:** oxlint-plugin-react-doctor 0.9.3 (prompt schema 2)

## Validation prompt

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

Fires when a CallExpression's callee resolves to a known external-store factory export AND enclosingComponentOrHookName is non-null (the call sits directly in the body of a PascalCase component or use* hook, including memo()-wrapped named/arrow components). The matched factories are zustand create/createStore, redux createStore, @reduxjs/toolkit configureStore/createSlice, jotai atom/createStore, valtio proxy, mobx observable/makeAutoObservable/makeObservable, nanostores atom/map, and @xstate/store createStore: matched only by named import (including renamed `create as makeStore`) or member call `zustand.create(...)`/`mobx.makeAutoObservable(...)` on a namespace/default import resolving to that exact module. By design the detector stays silent (does NOT fire) on: factories created inside an event handler `const onNew = () => create(...)` (re-run on click, not render) or inside a useMemo callback (memoized, not per render), since enclosingComponentOrHookName stops at the first inner function boundary; a locally-defined or shadowed `create`/`atom` that is not the imported factory; a same-named export from an unsupported module (`create` or `random.create()` from some-other-lib); subscription hooks useStore/useAtom (deliberately omitted from the factory list); and calls already at module scope or inside plain non-component helpers like `makeStore()`: these are correct exclusions, not false positives. False positive / blind spot (the rule's v2 out-of-scope gap): because this is a single-file scan it cannot follow cross-file resolution, so it MISSES a real violation when the factory is wrapped in a user-defined helper named like a hook/component (`function useMakeStore() { return create(...) }`, called once but re-detected per call) or when a store is constructed in and re-exported through another module; conversely, before confirming, verify the matched call truly re-runs on every render of the enclosing component/hook rather than being a one-shot wrapper.

## 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.

Hoist the factory call out of the component/hook to module scope so the store is allocated exactly once: replace `function App() { const useStore = create((set) => ({ count: 0 })); ... }` with `export const useStore = create((set) => ({ count: 0 }))` at the top of the file and reference it from the component. If the store genuinely must be per-instance (rare), construct it once with a stable ref or lazy init (`const storeRef = useRef(); if (!storeRef.current) storeRef.current = create(...)`) or inside a useMemo with a stable dependency list: never bare in the render body, since a fresh store every render disconnects subscribers, churns action-creator/reducer/store identities, and resets persisted state.

## 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/no-create-store-in-render` diagnostic in the current repository.

Required change:
- Hoist the factory call out of the component/hook to module scope so the store is allocated exactly once: replace `function App() { const useStore = create((set) => ({ count: 0 })); ... }` with `export const useStore = create((set) => ({ count: 0 }))` at the top of the file and reference it from the component. If the store genuinely must be per-instance (rare), construct it once with a stable ref or lazy init (`const storeRef = useRef(); if (!storeRef.current) storeRef.current = create(...)`) or inside a useMemo with a stable dependency list: never bare in the render body, since a fresh store every render disconnects subscribers, churns action-creator/reducer/store identities, and resets persisted state.

Validation before editing:
Fires when a CallExpression's callee resolves to a known external-store factory export AND enclosingComponentOrHookName is non-null (the call sits directly in the body of a PascalCase component or use* hook, including memo()-wrapped named/arrow components). The matched factories are zustand create/createStore, redux createStore, @reduxjs/toolkit configureStore/createSlice, jotai atom/createStore, valtio proxy, mobx observable/makeAutoObservable/makeObservable, nanostores atom/map, and @xstate/store createStore: matched only by named import (including renamed `create as makeStore`) or member call `zustand.create(...)`/`mobx.makeAutoObservable(...)` on a namespace/default import resolving to that exact module. By design the detector stays silent (does NOT fire) on: factories created inside an event handler `const onNew = () => create(...)` (re-run on click, not render) or inside a useMemo callback (memoized, not per render), since enclosingComponentOrHookName stops at the first inner function boundary; a locally-defined or shadowed `create`/`atom` that is not the imported factory; a same-named export from an unsupported module (`create` or `random.create()` from some-other-lib); subscription hooks useStore/useAtom (deliberately omitted from the factory list); and calls already at module scope or inside plain non-component helpers like `makeStore()`: these are correct exclusions, not false positives. False positive / blind spot (the rule's v2 out-of-scope gap): because this is a single-file scan it cannot follow cross-file resolution, so it MISSES a real violation when the factory is wrapped in a user-defined helper named like a hook/component (`function useMakeStore() { return create(...) }`, called once but re-detected per call) or when a store is constructed in and re-exported through another module; conversely, before confirming, verify the matched call truly re-runs on every render of the enclosing component/hook rather than being a one-shot wrapper.

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.
- 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.
````
