pankaj
pankaj

Reputation: 159

Metamask injects web3 after page refresh only

In my react application I am checking for web3 like below, but i need to refresh the page at least once after installing metamask on chrome.

detectAndReadMetaMaskAccount() {
        return new Promise((resolve, reject) => {
    if (typeof web3 === 'undefined' || typeof web3.currentProvider === 'undefined' || web3.currentProvider.isMetaMask !== true) {
                    reject({
                        code: "error.metamask.missing"
                    });
                }
}

Below are the steps I am doing:
1. loading my page.
2. Installing metamask on chrome.
3. Clicking on a submit button which invokes above function but web3 is undefined.
4. But after page refresh getting web3.

So my query is, when and how exactly metamask injects web3? Is there any way where metamask will inject web3 without page refresh?

Upvotes: 2

Views: 1672

Answers (1)

Christopher
Christopher

Reputation: 1455

Unfortunately I couldn't find any way to force web3 to update after installing MetaMask without a page reload.

As a hack workaround I'm forcing a page reload on window focus change:

window.onfocus = () => {
    window.location.reload();
}

The rationale behind this is that I open the page to install the MetaMask extension in new tab and the user needs to return to my tab before continuing the Onboarding process.

Upvotes: 1

Related Questions