Code Monkey home page Code Monkey logo

Comments (8)

philippe-git avatar philippe-git commented on July 29, 2024 3

Another more solid approach is available in the web3-react repo: the useEagerConnect hook. The code for that hook looks like this once adapted to use-wallet by using the web3-react instance exposed by useWallet:

import { useState, useEffect } from 'react';
import { useWallet } from 'use-wallet';

const useEagerConnect = () => {
  const { _web3ReactContext, connectors } = useWallet();
  const { activate, active } = _web3ReactContext;
  const injected = connectors.injected.web3ReactConnector({ chainId: CHAIN_ID });
  const [tried, setTried] = useState(false);

  useEffect(() => {
    injected.isAuthorized().then((isAuthorized) => {
      if (isAuthorized) {
        activate(injected, undefined, true).catch(() => {
          setTried(true);
        });
      } else {
        setTried(true);
      }
    });
  }, []); // intentionally only running on mount (make sure it's only mounted once :))

  // if the connection worked, wait until we get confirmation of that to flip the flag
  useEffect(() => {
    if (!tried && active) {
      setTried(true);
    }
  }, [tried, active]);

  return tried;
};

export {
  useEagerConnect,
};

from use-wallet.

MoukimHF avatar MoukimHF commented on July 29, 2024 2

This works fine for me

const { account, connect, reset, status } = useWallet();
React.useEffect(() => { if(localStorage.getItem("status")==="connected"){ connect("injected") } },[]);

const resetCb = () => { reset(); localStorage.setItem("status", "disconnected"); };

const connectCb = () => { connect("injected").then(() => { localStorage.setItem("status", "connected"); }) }

from use-wallet.

chiku524 avatar chiku524 commented on July 29, 2024

This is for the 'fortmatic' and 'portis' connectors, by the way. Haven't tested the other ones out yet

from use-wallet.

meirbank avatar meirbank commented on July 29, 2024

Same happens with meta mask. Would be great if we can connect automatically after user connects their wallet for the first time. This should be possible, on sites like Uniswap they do this.

from use-wallet.

vortextemporum avatar vortextemporum commented on July 29, 2024

Unfortunately I also encountered the same problem. It is the only reason I am not using this component for my project at the moment.

from use-wallet.

bpierre avatar bpierre commented on July 29, 2024

Agreed, it would be a great feature to have! Feel free to open a PR with it, otherwise I’ll have a look after #51 gets merged.

from use-wallet.

stoplion avatar stoplion commented on July 29, 2024

Has anyone found a way around this? I just added useWallet to my app, running into same issue

from use-wallet.

seniorjoinu avatar seniorjoinu commented on July 29, 2024

I just made a couple of functions like

export function setWalletConnected(value: keyof Connectors | null) {
  localStorage.setItem('__WALLET_CONNECTED', JSON.stringify(value));
}

export function isWalletConnected(): keyof Connectors | null {
  const val = localStorage.getItem('__WALLET_CONNECTED');

  return val ? JSON.parse(val) : null;
}

When a user connects their wallet, I call setWalletConnected('injected'). Then, when the app is loaded, I just do this

  const connectedId = isWalletConnected();
  const wallet = useWallet();
  if (wallet.status == 'disconnected' && connectedId != null) {
    wallet.connect(connectedId)
  }

I don't know if it works for other connectors, but Metamask's one works fine.

from use-wallet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.