Metamask injects web3 after page refresh only

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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) => 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?




1 Answer
1



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard