react
runtime_error
ai_generated
true
警告:useEffect 不能返回除函数以外的任何内容,该函数用于清理。您返回了:undefined
Warning: useEffect must not return anything besides a function, which is used for clean-up. You returned: undefined
ID: react/useeffect-cleanup-return-non-function
90%修复率
90%置信度
1证据数
2024-01-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
根因分析
useEffect 回调返回了一个非函数值(例如来自异步函数的 undefined、Promise 或数字),而不是清理函数或什么都不返回。
English
The useEffect callback returns a non-function value (e.g., undefined from an async function, a Promise, or a number) instead of a cleanup function or nothing.
官方文档
https://react.dev/reference/react/useEffect解决方案
-
Define an async function inside the useEffect and call it without returning it. The useEffect callback itself should not be async.
-
If a cleanup is needed, return a function that performs the cleanup. If not, omit the return statement entirely.
无效尝试
常见但无效的做法:
-
Making the useEffect callback async directly (async () => { ... })
95% 失败
An async function always returns a Promise, which is not a cleanup function, triggering the warning.
-
Adding a return statement that returns a string or boolean to suppress the warning
90% 失败
Only a function (or nothing) is allowed; any other type will still cause the warning.
-
Wrapping the entire useEffect in a try-catch block
75% 失败
The warning is about the return type of the callback, not about errors inside it; try-catch doesn't change the return value.