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

Why is connector.deactivate always undefined? #909

Open
zhaoyi0113 opened this issue Feb 4, 2024 · 1 comment
Open

Why is connector.deactivate always undefined? #909

zhaoyi0113 opened this issue Feb 4, 2024 · 1 comment

Comments

@zhaoyi0113
Copy link

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

floticerus commented Apr 13, 2024

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.

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

2 participants