I am trying to disconnect from the provider so that I can show a screen with a button to connect when not connected and show a display screen when already connected.
the example code doesn't work:
public resetApp = async () => {
const { web3 } = this.state;
if (web3 && web3.currentProvider && web3.currentProvider.close) {
await web3.currentProvider.close();
}
await this.web3Modal.clearCachedProvider();
this.setState({ ...INITIAL_STATE });
};
web3.currentProvider.close();
is not a function. the condition is never met.
all examples on the web doesn't work.
this code below always returns an address and nothing ive tried has cleared it except for manual disabling metamask and enable it again from the chrome extension settings.
const accounts = await web3.eth.getAccounts();
if (accounts[0] != null) {
// connected
resolve(true);
} else {
// not connected
resolve(false);
}
I am trying to disconnect from the provider so that I can show a screen with a button to connect when not connected and show a display screen when already connected.
the example code doesn't work:
public resetApp = async () => {
const { web3 } = this.state;
if (web3 && web3.currentProvider && web3.currentProvider.close) {
await web3.currentProvider.close();
}
await this.web3Modal.clearCachedProvider();
this.setState({ ...INITIAL_STATE });
};
web3.currentProvider.close();
is not a function. the condition is never met.
all examples on the web doesn't work.
this code below always returns an address and nothing ive tried has cleared it except for manual disabling metamask and enable it again from the chrome extension settings.
const accounts = await web3.eth.getAccounts();
if (accounts[0] != null) {
// connected
resolve(true);
} else {
// not connected
resolve(false);
}
Share
Improve this question
edited Aug 30, 2022 at 16:33
TylerH
21.1k79 gold badges79 silver badges114 bronze badges
asked Jul 6, 2021 at 14:18
loekTheDreamerloekTheDreamer
5861 gold badge8 silver badges25 bronze badges
2 Answers
Reset to default 1Found the solution: My condition was testing the wrong thing (not sure how else to say it).
If you call await web3Modal.clearCachedProvider();
then web3Modal.cachedProvider
returns an empty string.
// test if wallet is connected
if (web3Modal.cachedProvider) {
// connected now you can get accounts
const accounts = await web3.eth.getAccounts();
}
// disconnect wallet
const disconnectWallet = async (web3Modal: any) => {
await web3Modal.clearCachedProvider();
}
This will work;
async onDisconnect() {
await web3Modal.clearCachedProvider();
window.localStorage.clear();
}