当依赖性更改时,异步使用效应不会执行整个身体
我正在尝试将使用效率与某些异步函数一起使用,但是,当依赖项数组中的依赖性更改时,只有在使用效果中调用非异步代码。
useEffect( async () => {
console.log(account);
const web3 = await getWeb3();
const acc = await loadAcc(web3);
await loadContract(web3, acc);
}, [account])
当我的帐户状态变量更改时,使用效果再次调用,但只有Console.LOG(account)语句才能执行。
我应该如何解决这个问题?
I'm trying to use useEffect with some async functions, however when the dependency in the dependency array changes, only the non async code gets called in useEffect.
useEffect( async () => {
console.log(account);
const web3 = await getWeb3();
const acc = await loadAcc(web3);
await loadContract(web3, acc);
}, [account])
When my account state variable changes, useEffect gets invoked again, but only the console.log(account) statement will get executed.
How should I work around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将函数传递到
使用效果
不能是异步。您可以在使用效率内定义异步函数,然后使用当时的/捕获语法。此外,还将传递在使用效果之外定义的所有功能(您正在调用)作为使用效率的依赖性
The function passed into
useEffect
cannot be async. You can define an async function inside the useEffect and then use the then/catch syntax.Further, also pass in all the functions that are defined outside of the useEffect (that you are calling) as a dependency to useEffect
使用效果
期望返回void或函数(清理功能)。当您制作功能时,您将使用
作为异步,该函数将返回承诺。一种方法是,
否则
useEffect
expected to return either void or a function( the cleanup function ). When you make the function you pass touseEffect
as an async, the function will return a promise.One way to do it is,
Or else,