useState not updating connected wallet on account change - How to disconnect/logout?
Currently trying to determine why, on account change, the state of the selectedAddress variable is not updating.
This is the code:
const setProviderEvents = (newProvider) => {
newProvider.on("accountsChanged", async (accounts) => {
setSignedInAddress({ setSignedInAddress: accounts[0] });
});
newProvider.on("chainChanged", (chainId) => {
//(parseInt(chainId));
if (parseInt(chainId) != process.env.REACT_APP_CHAIN_ID) {
setInvalidChain(true);
} else {
setInvalidChain(false);
window.location.reload();
}
});
newProvider.on("disconnect", () => {
setSignedInAddress(null);
window.location.reload();
});
};
Secondly, how can you disconnect/logout of the current connection with WalletConnect?
const logoutOfWeb3Modal = useCallback(
async function () {
await web3Modal.clearCachedProvider();
setSignedInAddress(0);
window.location.reload();
},
[web3Modal]
);
But this doesn't update the DOM using setState either.
FYI, mark the start of your code block with ```javascript instead of just ``` Then you get:
const setProviderEvents = (newProvider) => {
newProvider.on("accountsChanged", async (accounts) => {
setSignedInAddress({ setSignedInAddress: accounts[0] });
});
newProvider.on("chainChanged", (chainId) => {
//(parseInt(chainId));
if (parseInt(chainId) != process.env.REACT_APP_CHAIN_ID) {
setInvalidChain(true);
} else {
setInvalidChain(false);
window.location.reload();
}
});
newProvider.on("disconnect", () => {
setSignedInAddress(null);
window.location.reload();
});
};
const logoutOfWeb3Modal = useCallback(
async function () {
await web3Modal.clearCachedProvider();
setSignedInAddress(0);
window.location.reload();
},
[web3Modal]
);
Stack Overflow is a better venue for your question.
With stable version 2.0.0 of Web3Modal now released, we are officially dropping support for version 1.x Due to this this issue/pr was marked for closing. It is highly recommended to upgrade as 2.x will be receiving further updates that will enable functionality for some of our newer sdks like auth and push as well as support for WalletConnect v2 (See this post about WalletConnect v1 being deprecated https://medium.com/walletconnect/walletconnect-v1-0-sunset-notice-and-migration-schedule-8af9d3720d2e)
If you need to continue using Web3Modal 1.x and require this feature/fix implemented, we suggest adding it via forking V1 branch.