简体   繁体   中英

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:

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

    // Your extra code here
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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