当我做这样的事情时
useEffect(() => {
if (steps.step2 && !requestKey) {
const timeoutId = setTimeout(() => startProcess(), 500)
return () => clearTimeout(timeoutId)
}
}, [steps, requestKey, startProcess])
Run Code Online (Sandbox Code Playgroud)
eslint抱怨我 error Expected to return a value at the end of arrow function consistent-return
的return () => clearTimeout(timeoutId)
因为当我对此发表评论时,错误消失了。
// return () => { clearTimeout(timeoutId); }
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题,有什么想法吗?
如果您在东边的一个分支中返回,则需要始终返回一个值以遵循规则。它不必是一个函数,它只需要是独立语句以外的东西return
。这也有效:
useEffect(() => {
if (steps.step2 && !requestKey) {
const timeoutId = setTimeout(() => startProcess(), 500)
return () => clearTimeout(timeoutId)
}
return undefined;
}, [steps, requestKey, startProcess])
Run Code Online (Sandbox Code Playgroud)
另请注意,setTimeout(() => startProcess(), 500)
简化为setTimeout(startProcess, 500)
.
归档时间: |
|
查看次数: |
1402 次 |
最近记录: |