阅读:2312回复:0
[前端]useEffect报错:useEffect must not return anything besides a function, which is used for clean-up.
如下代码:
useEffect(async() => { await test(); }, []); function test (){ return new Promise((resove,reject)=>{ setTimeout(function(){ setFormData({ remember: true, username:"doubleyong", password:"123" }) console.log("upload state") resove(); },2000) }); } 报错: Warning: useEffect must not return anything besides a function, which is used for clean-up. It looks like you wrote useEffect(async () => ...) or returned a Promise. Instead, write the async function inside your effect and call it immediately: 原因: 提示不能直接在useEffect中使用async, 报错信息中也给出了解决方式,就是把async放在useEffect里面,在立即调用 修改如下,重新运行这个警告就消失了。 解决方案: useEffect(()=>{ const temp = async() => { await test(); } temp() }, []); |
|
|