When refreshing the page account is undefined in react

I am using react-moralis and when I refresh the page the account value is not populated correctly and it is set to null.

const { account } = useMoralis();
const { chainId } = useChain();

I need to hard refresh multiple times to make it works again.

Any thought?

1 Like

you could try this: https://github.com/MoralisWeb3/react-moralis#refetch-user-data-refetchuserdata

It didn’t work.

I had to use user.get("ethAddress").

The issue with this solution though is that the user address doesn’t get updated if I am switching wallet.

Is there something I am missing?

are you using like the latest version of moralis and react-moralis?

“moralis”: “^0.0.184”,
“react-moralis”: “^0.3.11”,

I had other bugs with the latest version using etherjs. So @cryptokid advised me to use the last stable version.

this is from current user object, not from what is in metamask, so it will not get updated when wallet is switched

Ohhh I see, not aware of this issue, also yeah if you use user.get("ethAddress") the reason why it doesn’t change automatically is because it fetches from DB instead of detecting it locally

@cryptokid @YosephKS I know all of what you’re saying. My question is: why sometimes when I refresh my app, the account value is set to null and never updated again?

maybe it was not updated yet when it tried to display it, how do you display it?

I am using this code inside custom hook:

  useEffect(() => {
    if (!account) return;

   // Do something
  }, [account]);

This code is never run because account is always null and never updated

how do you initialize the account on refresh?

Only const {account} = useMoralis();

It should work no?

I don’t know, https://github.com/MoralisWeb3/react-moralis#usemoralis

there seem to be a lot of values from useMoralis, like isInitialized, isInitializing, user, enableWeb3, account, isWeb3Enabled

a lot of value yes but it would better if it’s actually working

maybe you need to use enableWeb3, check isInitialized, or isWeb3Enabled

Here what I am doing:


  const { isInitialized, Moralis } = useMoralis();

  useEffect(() => {
  }, [Moralis]);

  if (!isInitialized) {
    return <Loader />;

return <Component />


 useEffect(() => {
    if (!isAuthenticated) return;
    if (!account) return;

    // DO STUFF
  }, [account, isAuthenticated]);

I am always getting an empty account value with null

if you use console.log(account) on a button, it works?

why with a button? I have tried an it is the same

and btw, isWeb3Enabled is always false, even after calling Moralis.Web3.enableWeb3();

maybe that is why account is also undefined, if web3 is not enabled