📅  最后修改于: 2022-03-11 15:01:10.312000             🧑  作者: Mango
// The following code snippet is in React
// but you can use the window.ethereum.on("accountsChanged") event
// in regular JavaScript
// Note: I believe window.ethereum is only available if using Metamask
// as your web3 provider
useEffect(() => {
window.ethereum.on("accountsChanged", accounts => {
if (accounts.length > 0) setAccount(accounts[0]);
else setAccount("");
});
}, []);