Search code examples
reactjsblockchainweb3js

How to show current Metamask account


I want to show current Metamask account, so I set current metamask account like below.

async componentDidMount() {
  const ethaddress = await web3.eth.accounts[0] 
}

<p>{ethaddress}</p>

However, ethaddress never comes up in frontend.

In console command, if I type web3.eth.accounts[0], it shows current metamask account. But if I type ethaddress, it shows undefined.

Are there any suggestions to show the current metamask account?


Solution

  • Try to do something like this:

     web3.eth.getCoinbase(function (err, account) {
                if (err === null) {
                    App.account = account;
                    console.log("account", App.account);
                }
            })