use-wallet icon indicating copy to clipboard operation
use-wallet copied to clipboard

On page load, not connected

Open Dellybro opened this issue 4 years ago • 9 comments

I can't seem to figure out how to keep a wallet connected on page refresh.

Thanks

Dellybro avatar Feb 19 '21 19:02 Dellybro

connect("injected");

supermars01 avatar Mar 01 '21 12:03 supermars01

@supermars01 this doesn't seem to work for me, i call wallet.connect('injected') on my useEffect and the wallet account is null, but status is connected

Dellybro avatar Mar 04 '21 07:03 Dellybro

Yeah I get the same on hard reload. Account wont update

 // Check to see if we've set a provider in local Storage and connect
  const initProvider = async () => {
    if (provider) {
      console.log('Provider Found:', provider)
      await connect(provider)
      registerProvider(ethereum)
    }
  }
  
  // Once loaded, handoff provider & connect to wallet
  useEffect(() => {
    initProvider()
  }, [])

l3wi avatar Mar 07 '21 14:03 l3wi

@Dellybro Okay found a hacky way to make it work.

Watch for the status to become connected and if the account is still null, call await connect() again

l3wi avatar Mar 07 '21 15:03 l3wi

The best way to do this is when the user first connects to a wallet, i.e. after you have called wallet.connect() and wallet.status === "connected", save the value of wallet.connector in to local storage.

I'm using Gatsby so I'm overriding wrapPageElement with;

const Web3Connect = ({ children }) => {
    const wallet = useWallet()

    useEffect(() => {
        const cachedProvider = getItem(CACHED_PROVIDER_KEY)
        if (cachedProvider !== null) {
            wallet.connect("injected")
        }
    }, [])

    return children
}

const wrapPageElement = ({ element }) => <Web3Connect>{element}</Web3Connect>

eth0izzle avatar Apr 16 '21 12:04 eth0izzle

I also encountered this problem. Can I only reconnect after each page refresh?

Turing-bei avatar Jun 01 '21 07:06 Turing-bei

I have same issue.

bluedragon1120 avatar Sep 27 '21 10:09 bluedragon1120

The best way to do this is when the user first connects to a wallet, i.e. after you have called wallet.connect() and wallet.status === "connected", save the value of wallet.connector in to local storage.

I'm using Gatsby so I'm overriding wrapPageElement with;

const Web3Connect = ({ children }) => {
    const wallet = useWallet()

    useEffect(() => {
        const cachedProvider = getItem(CACHED_PROVIDER_KEY)
        if (cachedProvider !== null) {
            wallet.connect("injected")
        }
    }, [])

    return children
}

const wrapPageElement = ({ element }) => <Web3Connect>{element}</Web3Connect>

Did you solve this problem?

bluedragon1120 avatar Sep 27 '21 11:09 bluedragon1120

Ok, here's what worked for me.

Only call wallet.connect in ONE file. I did it in my main layout script that surrounds everything else.

In every other file, do something like this:

const wallet = useWallet();
useEffect(() => {
  if(wallet.status === 'connected') {
    doStuff();
  }
}, [wallet.status]);

roxworks avatar Oct 26 '21 13:10 roxworks