Accepted answer

From the docs,

Does useEffect run after every render? Yes! By default, it runs both after the first render and after every update. (We will later talk about how to customize this.) Instead of thinking in terms of “mounting” and “updating”, you might find it easier to think that effects happen “after render”. React guarantees the DOM has been updated by the time it runs the effects.

You can provide the empty dependency array / [] as second argument to useEffect, it is same as componentDidMount which will executes only once in component life cycle.

useEffect(() => {
}, []); //This will run only once 


Pass empty [] as an second argument to useEffect method. It will be called on initial render, like below.

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

Related Query

More Query from same tag