# 类型错误：无法读取 null 的属性（读取 'focus'）当使用 ref 回调时

- **ID:** `react/ref-callback-null-on-unmount`
- **领域:** react
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

当组件卸载时，ref 回调接收 null，而代码在未检查 null 的情况下尝试访问 ref 的属性或方法。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| React 18.2.0 | active | — | — |
| React 17.0.2 | active | — | — |

## 解决方案

1. ```
   Add a null check before accessing properties on the ref. Use optional chaining or a guard clause.
   ```
2. ```
   Use a ref callback with cleanup to handle the null case gracefully, especially when using third-party libraries.
   ```

## 无效尝试

- **** — useRef still holds a reference to the DOM node but can become stale; accessing .current after unmount may still be null or cause memory leaks. (50% 失败率)
- **** — setTimeout doesn't guarantee the component is still mounted; if unmount occurs during the delay, ref.current is null. (80% 失败率)
