enableWeb3() is not working

I have updated Moralis SDK and react Moralis to the latest version, Since then enableWeb3() is not enabling web3 instance. I have tried reactMoralis’s enableWeb3() method and Moralis.enableWeb3(). Moralis.enableWeb3() throws an error “Cannot read properties of null (reading ‘on’)”.

if (!isWeb3Enabled && !isWeb3EnableLoading) {

         Moralis.enableWeb3();

         //enableWeb3({ provider: localStorage.getItem("provider") });

         //Moralis.enableWeb3({ provider: localStorage.getItem("provider") })

       }


React Moralis >> v1.0.4

Moralis >> v1.0.7
node >> v16.13.0
npm >> v8.1.0

Edit: enable web3 from react moralis is working perfectly.

You could read/post in this threat: Moralis JS-SDK v1.0.0 [beta] (Ethers.js support)

1 Like

If you use react, then this code is prone to errors, as it might get called multiple times in a row (depending on how often the component is re-rendered).

I only could reproduce this error by calling enableWeb3 multiple times right after each other, which will result in Moralis creating AND cleaning up a web3 instance at the same time.

We will add some better checking, but the better implementation will be something like this in React:

useEffect(()=>{
  if (!isWeb3Enabled && !isWeb3EnableLoading) {
    enableWeb3();
  }
},[isWeb3Enabled, isWeb3EnableLoading])
5 Likes