phantom / docs

32 stars 17 forks source link

solana object is not found on initial load (React) #16

Closed adilanchian closed 1 year ago

adilanchian commented 2 years ago

Browser: Chrome 95.0.4638.54

Hi there!

I'm using Phantom Wallet extension in my React App. I am trying to check to see if the user has Phantom installed. If they don't send them a message saying to install it.

Currently I just check the window object directly on component mount like so:

if (window.solana) {
    // Do something with solana object
} else {
    // Tell user to download phantom wallet
}

A few things to note here:

Curious if you have come across this :)? Thanks!

adilanchian commented 2 years ago

Quick update on this!

Since it seemed like this was just taking a bit of time to be injected into the window, I decided to actually do a check for the solana object after the window has loaded (Thanks for the suggestion Francesco 🤘)

This has seemed to do the trick for me (using React.js) -

useEffect(() => {
  window.addEventListener('load', async (event) => {
    if (window.solana) {
      // Do something with solana object
    } else {
      // Tell user to download phantom wallet
    }
  });
}, []);

Hope this helps!