[react-moralis] Unsubscribe event in useEffect [SOLVED]

Hi All. I have used onAccountsChanged and wanted to unsubscribe in my return for useEffect hook but in the console I received UNSUB NOT SUPPORTED … Why is that ? Thanks

useEffect(() => {
    const unsubscribe = Moralis.Web3.onAccountsChanged(function (accounts) {
      console.log(accounts);
    });

    return () => {
      unsubscribe();
    };
  }, [Moralis.Web3]);

Hi @Michal

Could you share the code how you import Moralis. From useMoralis() hook?

Hi @Yomoo,

const {
    authenticate,
    isAuthenticated,
    isAuthenticating,
    user,
    Moralis,
    logout,
    authError,
    web3,
  } = useMoralis();

@Yomoo I think I found the problem
I was using for one of my useEffect

useEffect(() => {
    const getChainId = async () => {
      const web3 = await Moralis.Web3.enable();
      if (web3.currentProvider) {
        setChainId(web3.currentProvider.chainId);
      }
    };

    getChainId();
  }, [Moralis.Web3]);

and I should use web3 from useMoralis() and my use effect should look like this

useEffect(() => {
    const getChainId = () => {
      if (web3.currentProvider) {
        setChainId(web3.currentProvider.chainId);
      }
    };

    getChainId();
  }, [web3]);

Now I do not have any warning or error in my console.

1 Like

So does it work correctly now? :star_struck:

Yes it does without any problem. Thanks

1 Like

Great job! :star_struck:

1 Like