# TypeError: 无法读取 undefined 的属性 'setState'

- **ID:** `react/error-cannot-read-properties-of-undefined-reading-setstate`
- **领域:** react
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

类组件试图调用 this.setState，但 'this' 是 undefined，通常是因为方法没有绑定到实例或组件没有正确实例化。

## 版本兼容性

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

## 解决方案

1. ```
   Bind the method in the constructor: constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); }
   ```
2. ```
   Use class property arrow functions: handleClick = () => { this.setState(...); }
   ```
3. ```
   Convert the class component to a function component with useState hook to avoid 'this' issues entirely.
   ```

## 无效尝试

- **Use arrow functions in JSX event handlers without binding** — Arrow functions in JSX create a new function on every render, causing performance issues and still not fixing the 'this' context if the method is not bound. (80% 失败率)
- **Call setState as a standalone function without 'this'** — setState is a class instance method and must be called on 'this'; calling it standalone will throw a different error. (100% 失败率)
- **Use functional setState syntax (prevState => newState) but still without binding** — The functional syntax doesn't solve the 'this' binding issue; the method still needs to be called on the correct context. (95% 失败率)
