[SOLVED] For loop from useMoralisQuery not loading on useEffect on first render

problem is in useEffect it is not being called on first render.
when I check console.log it is not being rendered on page load still

const [totalStaked, setTotalStaked] = useState(0);

  useEffect(() => {

    if (isAuthenticated) {
       enableWeb3();

        lockedTransactions.forEach((e) => {
        const totalAmountStaked = (Number(e.attributes.staked) + Number(e.attributes.staked));
        setTotalStaked(totalAmountStaked);
    
        })
    }
  }, [isAuthenticated]);

You would need to use a useEffect with an empty dependency array:

useEffect(() => {

}, []);

But this may not be what you actually need. What is it that you’re wanting to do?

1 Like

Thanks glad! I tried with empty array it doesn’t work because I need to use isAuthenticated

Solve this upon putting totalAmountStaked in dependency array.

Solved:

const [totalStaked, setTotalStaked] = useState(0);

  useEffect(() => {

    if (isAuthenticated) {
       enableWeb3();

        lockedTransactions.forEach((e) => {
        const totalAmountStaked = (Number(e.attributes.staked) + Number(e.attributes.staked));
        setTotalStaked(totalAmountStaked);
    
        })
    }
  }, [isAuthenticated, totalAmountStaked]);

Thank you!