I tried out onAccountsChanged event like given below it sometimes triggers multiple times when I switch between accounts in metamask and shows to authenticate in metamask multiple times when I try to link an account.
Is there there anything wrong with the code below? Is there a way to fix this behaviour
useEffect(() => {
if (isAuthenticated && isWeb3Enabled) {
Moralis.Web3.onAccountsChanged(async (accounts) => {
setIsLoading(true)
if (await checkIfLinked(accounts[0])) {
await setUserData({ "ethAddress": accounts[0] });
} else {
await Moralis.Web3.link(accounts[0]);
await setUserData({ "ethAddress": accounts[0] });
}
});
}
}, [isAuthenticated,isWeb3Enabled]);