react
runtime_error
ai_generated
true
effect 函数不能返回除函数之外的任何内容,该函数用于清理。你返回了:undefined
An effect function must not return anything besides a function, which is used for clean-up. You returned: undefined
ID: react/useEffect-return-not-function
92%修复率
90%置信度
1证据数
2023-08-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| React 16.8+ | active | — | — | — |
| React 17.x | active | — | — | — |
| React 18.x | active | — | — | — |
根因分析
useEffect 回调返回了一个不是函数或 undefined 的值,违反了 React 对 effect 清理的约定。
English
The useEffect callback returns a value that is not a function or undefined, violating React's contract for effect cleanup.
官方文档
https://react.dev/reference/react/useEffect#return解决方案
-
确保 useEffect 回调不返回任何内容(undefined)或返回一个清理函数。示例:useEffect(() => { const timer = setInterval(...); return () => clearInterval(timer); }, []); -
如果需要异步逻辑,在 effect 内部定义异步函数并调用它,但不要返回 promise。示例:useEffect(() => { const fetchData = async () => { ... }; fetchData(); }, []);
无效尝试
常见但无效的做法:
-
Returning null or false from useEffect to suppress the warning.
90% 失败
React expects either undefined (no cleanup) or a function; returning null/false triggers the same error because they are not functions.
-
Removing the return statement entirely even when cleanup is needed.
70% 失败
Without a cleanup function, subscriptions, timers, or event listeners may persist, causing memory leaks or duplicate effects.
-
Wrapping the return value in an async function (e.g., useEffect(async () => { ... return someValue; })).
95% 失败
Async functions always return a Promise, which is not a function for cleanup; React will throw the same error.