{
  "id": "react/stale-closure-useeffect",
  "signature": "Warning: A stale closure was detected. The callback passed to useEffect references a variable that may be outdated.",
  "signature_zh": "警告：检测到过时的闭包。传递给 useEffect 的回调引用了一个可能已过时的变量。",
  "regex": "Warning: A stale closure was detected",
  "domain": "react",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A useEffect callback captures a variable from an earlier render, and when that variable changes, the effect still uses the old value because the dependency array is missing or incomplete.",
  "root_cause_type": "generic",
  "root_cause_zh": "useEffect 回调捕获了来自之前渲染的变量，当该变量发生变化时，由于依赖数组缺失或不完整，effect 仍然使用旧值。",
  "versions": [
    {
      "version": "React 16.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "React 17.0.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "React 18.2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The effect never re-runs when the captured variable changes, so the closure remains stale indefinitely.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The variable is still captured at the time of effect creation; if it changes, the effect still uses the old value.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the ref is not manually updated when the variable changes, it still holds the old value.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add all variables referenced inside the effect to the dependency array: `useEffect(() => { console.log(count); }, [count]);`",
      "success_rate": 0.9,
      "how": "Add all variables referenced inside the effect to the dependency array: `useEffect(() => { console.log(count); }, [count]);`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use the functional update form of setState or useReducer if the stale variable is a state value: `setCount(prev => prev + 1)`",
      "success_rate": 0.85,
      "how": "Use the functional update form of setState or useReducer if the stale variable is a state value: `setCount(prev => prev + 1)`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `useRef` to hold the latest value and update it via an effect: `const countRef = useRef(count); countRef.current = count;` then access `countRef.current` inside the effect.",
      "success_rate": 0.8,
      "how": "Use `useRef` to hold the latest value and update it via an effect: `const countRef = useRef(count); countRef.current = count;` then access `countRef.current` inside the effect.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Add all variables referenced inside the effect to the dependency array: `useEffect(() => { console.log(count); }, [count]);`",
    "Use the functional update form of setState or useReducer if the stale variable is a state value: `setCount(prev => prev + 1)`",
    "Use `useRef` to hold the latest value and update it via an effect: `const countRef = useRef(count); countRef.current = count;` then access `countRef.current` inside the effect."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-05-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}