I am trying to add this code to handle the change of accounts in Metamask
...
ethereum
.request({ method: 'eth_accounts' })
.then(handleAccountsChanged)
.catch((err: any) => {
// Some unexpected error.
// For backwards compatibility reasons, if no accounts are available,
// eth_accounts will return an empty array.
console.error(err);
});
if (ethereum) {
// running on client and window + ethereum is avail
console.log("client")
} else {
// you are on the server.
// OR user does not have a broswer wallet - i.e. metamask
}
// Note that this event is emitted on page load.
// If the array of accounts is non-empty, you're already
// connected.
ethereum.on('accountsChanged', handleAccountsChanged);
...but I am getting the following error in my TypeScript
project:
TypeError: Cannot read properties of null (reading 'request')
ethereum
has been defined like this, using metamask-react
:
const { status, connect, account, chainId, ethereum } = useMetaMask();
It seems that the object ethereum
is not being correctly loaded.
Any thoughts?