【发布时间】:2022-01-07 00:38:25
【问题描述】:
我的 react 组件外部有一个 async 函数,它从 2 个地址检索 2 个令牌余额。我想在注释掉的 HTML 行中渲染出 2 个地址的余额。特别是linkBalance 和balance。 useContext 钩子我想也许我可以使用 context 解决我的问题,但我不确定。
import { ethers } from 'ethers'
const { ethereum } = window
const provider = new ethers.providers.Web3Provider(window.ethereum);
const linkAddress = '0xa36085F69e2889c224210F603D836748e7dC0088'
const linkABI = require('../constants/erc20.json')
const linkContract = new ethers.Contract(linkAddress, linkABI, provider);
(async() => {
const accounts = await ethereum.request({ method: 'eth_accounts' })
const account = accounts[0]
const balance = await provider.getBalance(account)
console.log(ethers.utils.formatUnits(balance, 18))
const linkBalance = await linkContract.balanceOf(contractAddress)
console.log(ethers.utils.formatUnits(linkBalance, 18))
})()
const Main = () => {
const { connectWallet, currentAccount } = useContext(PackPlayersContext)
return(
<>
<div>
{currentAccount && (<p>Current Address: {currentAccount}</p>)}
{!currentAccount && (
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={connectWallet}>Connect Wallet</button>
)}
<input className="shadow appearance-none border rounded w-half py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="username" type="text" placeholder="Price"/>
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mb-4 mt-4">Buy Pack</button>
{/*
<div>
<p>Current link balance of contract: {linkBalance}</p>
<p>Your KETH balance: {balance}</p>
</div>
*/}
</div>
</>
)
}
export default Main
【问题讨论】:
-
为什么异步函数必须在反应组件之外?
-
为什么要在组件之外设置异步函数?我假设您想在渲染组件之前运行您的函数,如果是这种情况,您应该使用 useEffect 钩子来执行此操作。
-
你可以像这样作弊和定义变量,window.newvariable = "hello";并且所有函数都可以在最高级别看到这个范围。
-
我似乎已经解决了这个问题,看看下面,我不确定我是否以最有效的方式做到了。
标签: javascript reactjs ethers.js