【发布时间】:2020-09-08 22:39:12
【问题描述】:
每次在metamask中更改帐户时,我都会尝试更新地址和余额,但是只有帐户在更改并且余额没有更新。
async componentDidMount(){
let accs = await web3.eth.getAccounts();
let balance = await web3.eth.getBalance(accs[0]);
let balanceth = await web3.utils.fromWei(balance,'ether');
console.log(balanceth+" ether");
this.setState({ balanceth });
this.setState({ account:accs[0] });
window.ethereum.on('accountsChanged', (accounts) => {
console.log(accounts[0]);
this.setState({ account:accounts[0] });
let acc = this.state.account;
// Time to reload your interface with accounts[0]!
web3.eth.getBalance(acc,(err,bal)=> {balance = bal})
console.log(balance)
web3.utils.fromWei(balance,'ether',(err,bal)=>{balanceth = bal});
console.log(acc,balanceth);
this.setState({ balanceth:balanceth });
})
}
【问题讨论】:
-
你能显示你的输入字段的html吗?