UseEffect being called multiple times

Your useEffect is executed only once per render cycle, but you have several state updates in your useEffect which cause a re-render. Hence you get a lot of alerts.

See a demo of your code and see the console.logs as well as comments

Also note that useEffect will

  • when you provide empty array dependency, your useEffect execute once
  • when you some value as dependency (eg: [name] ), your useEffect execute when name state/prop changes
  • useEffect executes on every re-render if you don’t pass the dependency array.

Read here on re-render

Leave a Comment