am trying to build a web3 application. In my frontend I want to detect if user changed his account on Metamask wallet or disconnected and want to show this change on frontend. How can I do so?
const [account, setaccount] = useState('0x0');
window.ethereum.on('accountsChanged',async (accounts) =>{
const changedAccounts =await window.ethereum.request({method: 'eth_requestAccounts'});
setaccount(changedAccounts[0]);
console.log('changedAccounts')
})
useEffect(async () => {
if(!window.ethereum){
window.alert('Please consider installing Metamask');
}else{
const accounts =await window.ethereum.request({method: 'eth_requestAccounts'});
setaccount(accounts[0]);
}
if(!window.ethereum.isConnected()){
console.log('Disconnected')
}else{
console.log('Connected')
}
console.log('useEffect')
}, [account]);