2 years ago

#66833

test-img

Gaming Hacker

Unable to detect window.ethereum on Metamask mobile

I am unable to detect window.ethereum in Metamask mobile but works fine in metamask extension and Trust Wallet. I am using react with vite.

Also I am writing code in repl.it ide with react template.

Here is the full code.

import React from 'react';
import { ethers } from "ethers";

function dosomething(){
  if(window.ethereum){
    const provider = new providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();
  }
  else{
            alert('Please install MetaMask first.');
        }
}
function NewFun() {
  if (window.ethereum) {
  dosomething();
}
    else {
  window.addEventListener('ethereum#initialized', dosomething, {
    once: true,
  });

  // If the event is not dispatched by the end of the timeout,
  // the user probably doesn't have MetaMask installed.
  setTimeout(dosomething, 5000); // 5 seconds
}
}

function Test() {
  return(
    <main>
      <button onClick={NewFun}>Hiii</button>
      Hii
    </main>);
}

export default Test;

Basic Html , js works fine.

reactjs

metamask

web3-react

0 Answers

Your Answer

Accepted video resources