Moralis.onAccountChange issue with latest Moralis SDK

const { Moralis } = useMoralis();

Can anyone explain why this happens?

Here is my versions of moralis and react-moralis modules

"moralis": "^1.3.2",
"react-moralis": "^1.3.1",

you can read here about how to use it in latest version of the SDK:

@Erno mentioned that we need to use Moralis.onAccountChange event after enableWeb3();
I tried that but it didn’t worked.

Here is my code,

useEffect(() => {
    if (isAuthenticated && isWeb3Enabled) {
      Moralis.onAccountChanged(async (accounts) => {
        const currentUser = Moralis.User.current();
        if (currentUser) {
          await setUserDataOnLinkStatus(accounts[0]);
        }
      });
      getBalance();
    }
    return () => {
      setIsLoading(false);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isAuthenticated, isWeb3Enabled]);

Here how we enable Web3

import { useMoralis, useChain } from "react-moralis";

const { isWeb3Enabled, isInitialized, enableWeb3, isAuthenticated, user, isWeb3EnableLoading, logout, Moralis, setUserData, web3, account } = useMoralis();

const enableWeb3Provider = () => {
    if (isAuthenticated && !isWeb3Enabled && !isWeb3EnableLoading) {
      const isWalletconnect = user.get("walletconnect");
      console.log("enableWeb3Provider isWalletconnect: ", isWalletconnect);
      if (isWalletconnect) {
        enableWeb3({ provider: "walletconnect" });
        return;
      }
      enableWeb3();
    }
  };

@cryptokid Is there any possible way to solve this issue?

I noticed this part in that in that forum thread:

Moralis.onXXX events have changed

Instead of subscribing to Metamask events directly, moralis generalizes these events so that they can be used for any connector: metamask, walletconnect, network etc. So now the events are fired from the connector that is use to authenticate/enableWeb3.

@cryptokid I observed that point and tried to use Moralis.onAccountChange accordingly.But it didn’t work for me.

did you use it with metamask or wallet connect?

I am using this with Metamask

what doesn’t work with metamask? did you try window.ethereum.on?

Moralis.onAccountChange is not working and this error is appearing when I use it.

window.ethereum.on is working but this error is appearing when I use it

maybe you can ignore that error

@cryptokid Do you have any idea why Moralis.onAccountChange event is not working?

I don’t know why is not working

Is this ever solved? I am having same problems

If you’re using it in a hook as above, you can have it in such way rather

useEffect(() => {
  console.log(account)
}, [account])