# Minified React error #329; 访问 https://reactjs.org/docs/error-decoder.html?invariant=329 获取完整消息，或使用非压缩的开发环境查看完整错误。

- **ID:** `react/error-minified-react-error-329`
- **领域:** react
- **类别:** runtime_error
- **错误码:** `329`
- **验证级别:** ai_generated
- **修复率:** 87%

## 根因

React 内部不变性违反 #329 表示状态更新被调用在一个已卸载的组件上，通常是由于异步操作（例如 setTimeout、fetch）在组件卸载后完成。

## 版本兼容性

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

## 解决方案

1. ```
   Use a cleanup function in useEffect to cancel asynchronous operations. Example: useEffect(() => { const controller = new AbortController(); fetch(url, { signal: controller.signal }); return () => controller.abort(); }, []);
   ```
2. ```
   Check if the component is mounted using a ref before calling setState. Example: const isMounted = useRef(true); useEffect(() => { return () => { isMounted.current = false; }; }, []); then check if (isMounted.current) before setState.
   ```
3. ```
   Use a state management library like Zustand or Redux that handles updates outside the component lifecycle, avoiding direct setState calls.
   ```

## 无效尝试

- **Use componentWillUnmount lifecycle method in a function component** — Function components do not have componentWillUnmount; you must use useEffect cleanup instead. (100% 失败率)
- **Add a try-catch around the state update call** — The error is not a thrown exception; it's an invariant violation that bypasses try-catch. (95% 失败率)
- **Call setState with a callback to ensure it's executed after the update** — setState callbacks don't prevent updates on unmounted components. (90% 失败率)
