web3-react
web3-react copied to clipboard
Why is `connector.deactivate` always undefined?
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>
);
};
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.