Uniswap / web3-react

A simple, maximally extensible, dependency minimized framework for building modern Ethereum dApps
https://web3-react-mu.vercel.app/
GNU General Public License v3.0
5.48k stars 1.51k forks source link

Why is `connector.deactivate` always undefined? #909

Open zhaoyi0113 opened 5 months ago

zhaoyi0113 commented 5 months ago

I am using web3-react to connect to Metamask and it works perfect, I am able to get the accounts and balances from the connector. However, I found the deactivate method in the connector instance is always undefined. I have put my code as below, the if (connector.deactivate) { condition is always false.

Is there anything I did wrong?

const Navbar = () => {
  const { connector, hooks } = useWeb3React();
  const { useSelectedAccount, useSelectedProvider, useSelectedIsActivating } = hooks;
  const { useAccounts } = useMetaMask;

  const account = useSelectedAccount(connector);
  const provider = useSelectedProvider(connector);
  const accounts = useAccounts();
  const [balance, setBalance] = useState<string>('0');
  const [isConnected, setConnected] = useState<boolean>(false);
  console.log('account', account, accounts);
  provider?.getNetwork().then((n) => console.log('network', n));
  if (account) {
    provider?.getBalance(account).then((b) => {
      setBalance(formatEther(b));
      console.log('account balance', balance);
    });
  }
  console.log('ens address:', provider?.network?.ensAddress);
  const connect = async () => {
    const chainId = Web3.utils.toHex(5);
    console.log('connect to chain id', chainId, connector);
    try {
      await connector.activate();
      setConnected(true);
    } catch (err) {
      console.error('User rejected the request', err);
    }
  };

  const disconnect = async () => {
    if (connector.deactivate) {
      await connector.deactivate();
    }
  };
  return (
    <Nav>
      <Links>
        <A>Home</A>
        <A>Resources</A>
        <A>About us</A>
      </Links>
      {isConnected ? <Button onClick={disconnect}>Disconnect</Button> : <Button onClick={connect}> Connect </Button>}
    </Nav>
  );
};
floticerus commented 2 months ago

Having the same problem... deactivate is always undefined, which makes it impossible to disconnect?

I assume this repo is dead at this point. Thanks, Uniswap.

FWIW - I run my own logic for auto-connecting, so in my disconnect button I disable the auto-connect and reload the page, which seems to be working. Absolutely not ideal, but maybe it will help somebody.