{
  "id": "react/stale-closure-usestate",
  "signature": "Warning: State update from an outdated closure. The value you are accessing is stale.",
  "signature_zh": "警告：来自过时闭包的状态更新。您访问的值已过时。",
  "regex": "Warning: State update from an outdated closure\\..*stale",
  "domain": "react",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A state setter or callback captures a stale closure value (e.g., from an event handler or setTimeout) that does not reflect the latest state, often due to missing dependency arrays in useEffect or useCallback.",
  "root_cause_type": "generic",
  "root_cause_zh": "状态设置器或回调捕获了过时的闭包值（例如，来自事件处理程序或setTimeout），该值未反映最新状态，通常是由于useEffect或useCallback中缺少依赖项数组。",
  "versions": [
    {
      "version": "React 18.2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "React 19.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding the state variable to the dependency array of useEffect without using functional update",
      "why_fails": "Adding the state variable may cause infinite re-renders if the effect updates state, and does not fix the closure if the callback is defined outside the effect.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using a ref to store the state value and reading from ref in the closure",
      "why_fails": "This works but is often overcomplicated; many developers misuse refs by not syncing them properly, leading to stale ref values.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring the warning and assuming it's a false positive",
      "why_fails": "The warning indicates a real bug; ignoring it leads to unpredictable UI behavior and hard-to-debug state inconsistencies.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use the functional update form of setState: setCount(prev => prev + 1) instead of setCount(count + 1), and ensure all dependencies are listed in useEffect or useCallback dependency arrays.",
      "success_rate": 0.85,
      "how": "Use the functional update form of setState: setCount(prev => prev + 1) instead of setCount(count + 1), and ensure all dependencies are listed in useEffect or useCallback dependency arrays.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If using useCallback, include all state variables in the dependency array to ensure the callback is recreated when state changes.",
      "success_rate": 0.8,
      "how": "If using useCallback, include all state variables in the dependency array to ensure the callback is recreated when state changes.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Use the functional update form of setState: setCount(prev => prev + 1) instead of setCount(count + 1), and ensure all dependencies are listed in useEffect or useCallback dependency arrays.",
    "If using useCallback, include all state variables in the dependency array to ensure the callback is recreated when state changes."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://react.dev/reference/react/useState#stale-closure",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-11-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}