📜  检查 web3 元掩码断开连接 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:10.312000             🧑  作者: Mango

代码示例1
// 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("");
    });
}, []);