简体   繁体   English

未安装元掩码时 React 应用程序不呈现

[英]React app does not render when metamask is not installed

I get this error in the console whenever metamask is not installed in the browser.:每当浏览器中未安装元掩码时,我都会在控制台中收到此错误。:

Uncaught TypeError: window.ethereum is undefined

It's all just a black screen一切都只是黑屏

The simple solution is to wrap anything that references window.ethereum with an if:简单的解决方案是使用 if 包装任何引用window.ethereum的内容:

useEffect(() => {
  if(window.ethereum) {
    window.ethereum.on('chainChanged', () => {
      window.location.reload();
    })
    window.ethereum.on('accountsChanged', () => {
      window.location.reload();
    })

    // Your extra code here
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM