DEV Community

Discussion on: What is useEffect Hook in ReactJS?How useEffect works and where to use it?

Collapse
devparkk profile image
Dev Prakash

It is not guaranteed that useEffect function will be called on every rerender .
But in the following case it will :
If you pass some state in the dependency array , so whenever that state changes its value then it would mean two things to the react . changing this value will cause useEffect function to be called again , and also since this is a state , it will rerender the component .
Order of execution will be( after initial render ) :
1 . New component will be rendered and it will be displayed on to the browser (also called painted on to the browser )

2 . if you have defined some cleanup function , then cleanup function will be called for the previous component .
3 . At last useEffect function will be called for this new component .

There is this beautiful and comprehensive article on the same by Dan Abramov . overreacted.io/a-complete-guide-to...
Take a look for better understanding .

Happy coding