Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On page load, not connected #82

Open
Dellybro opened this issue Feb 19, 2021 · 9 comments
Open

On page load, not connected #82

Dellybro opened this issue Feb 19, 2021 · 9 comments

Comments

@Dellybro
Copy link

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

Thanks

@supermars01
Copy link

connect("injected");

@Dellybro
Copy link
Author

Dellybro commented Mar 4, 2021

@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

@l3wi
Copy link

l3wi commented Mar 7, 2021

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
Copy link

l3wi commented Mar 7, 2021

@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

@eth0izzle
Copy link

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>

@Turing-bei
Copy link

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

@bluedragon1120
Copy link

I have same issue.

@bluedragon1120
Copy link

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?

@roxworks
Copy link

roxworks commented Oct 26, 2021

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]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants