Search code examples
reactjsuse-effect

React uesEffect is not run on initial render


My list was loading to begin with but now suddenly useEffect will not run the function and my list is empty..

What am I missing?

Here is my code in codesandbox


Solution

  • Thats because first your component returns, rendering your JSX and then he calls getList inside your useEffect.

    But on your initial render, hes trying to get some properties that dont exists yet, throwing error.

    Try to use optional chaining operator in all of your liste[property], they are:

    liste?.[id]?.navn and liste?.[id]?.subtotal

    With these modifications, your component will follow the flow you expecting.