TypeError: Cannot read properties of null (reading 'givenProvider') in web3.givenProvider?.selectedAddress

providers\MoralisDappProvider\MoralisDappProvider.tsx (24:13) @ eval

  22 |   () =>
  23 |     setWalletAddress(
> 24 |       web3.givenProvider?.selectedAddress || user?.get("ethAddress")
     |           ^
  25 |     ),
  26 |   [web3, user]
  27 | );

Assuming that web3 is already defined and can your share some more details of the code instead of just the error.

import React, { useEffect, useMemo, useState } from "react";
import { useMoralis } from "react-moralis";
import MoralisDappContext from "./context";

function MoralisDappProvider({ children }) {
  const { web3, Moralis, user } = useMoralis();
  const [walletAddress, setWalletAddress] = useState();
  const [chainId, setChainId] = useState();
  useEffect(() => {
   ( Moralis as any).onChainChanged(function (chain) {
      setChainId(chain);
    });
//secondaryOpacity
   ( Moralis as any).onAccountsChanged(function (address) {
      setWalletAddress(address[0]);
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  useEffect(() => setChainId(web3.givenProvider?.chainId));
  useMemo(
    () =>
      setWalletAddress(
        web3.givenProvider?.selectedAddress || user?.get("ethAddress")
      ),
    [web3, user]
  );

  return (
    <MoralisDappContext.Provider value={{ walletAddress, chainId: "0x1" }}>
      {children}
    </MoralisDappContext.Provider>
  );
}

function useMoralisDapp() {
  const context = React.useContext(MoralisDappContext);
  if (context === undefined) {
    throw new Error("useMoralisDapp must be used within a MoralisDappProvider");
  }
  return context;
}

export { MoralisDappProvider, useMoralisDapp };

I can’t seem to figure out what is causing this error.
web3 do not have givenProvider property

1 Like

Where did you find the use of givenProvider? Start from there. There is no mention of it anywhere on https://github.com/MoralisWeb3/react-moralis.