{
  "id": "react/use-callback-stale-closure",
  "signature": "Warning: A stale closure was detected. The callback passed to useCallback references a variable that may be outdated.",
  "signature_zh": "警告：检测到过时闭包。传递给 useCallback 的回调引用了一个可能已过时的变量。",
  "regex": "Warning: A stale closure was detected\\. The callback passed to useCallback references a variable that may be outdated\\.",
  "domain": "react",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The useCallback hook captures a variable in its closure that is not listed in its dependency array, causing the callback to reference a stale value.",
  "root_cause_type": "generic",
  "root_cause_zh": "useCallback 钩子在其闭包中捕获了一个未在依赖数组中列出的变量，导致回调引用了过时的值。",
  "versions": [
    {
      "version": "React 18.2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "React 19.0.0-rc.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This ignores the stale closure issue and may still reference outdated values, leading to bugs.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "useMemo is for values, not callbacks, and does not solve the dependency tracking issue.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This forces the callback to always be stale, breaking any logic that relies on current state.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add the missing variable to the dependency array of useCallback. For example: const handleClick = useCallback(() => { console.log(count); }, [count]);",
      "success_rate": 0.95,
      "how": "Add the missing variable to the dependency array of useCallback. For example: const handleClick = useCallback(() => { console.log(count); }, [count]);",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a ref to store the variable and access it inside the callback without adding it to dependencies. Example: const countRef = useRef(count); countRef.current = count; const handleClick = useCallback(() => { console.log(countRef.current); }, []);",
      "success_rate": 0.85,
      "how": "Use a ref to store the variable and access it inside the callback without adding it to dependencies. Example: const countRef = useRef(count); countRef.current = count; const handleClick = useCallback(() => { console.log(countRef.current); }, []);",
      "condition": "",
      "sources": []
    },
    {
      "action": "Upgrade to React 19 and use the new use hook which automatically tracks dependencies.",
      "success_rate": 0.9,
      "how": "Upgrade to React 19 and use the new use hook which automatically tracks dependencies.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Add the missing variable to the dependency array of useCallback. For example: const handleClick = useCallback(() => { console.log(count); }, [count]);",
    "Use a ref to store the variable and access it inside the callback without adding it to dependencies. Example: const countRef = useRef(count); countRef.current = count; const handleClick = useCallback(() => { console.log(countRef.current); }, []);",
    "Upgrade to React 19 and use the new use hook which automatically tracks dependencies."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://react.dev/reference/react/useCallback",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-11-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}