React question detail
When and how often does React invoke the setup and cleanup functions inside a useEffect hook?
- Setup Function Execution (
useEffect)
The setup function (or the main function) you pass to useEffect runs at specific points:
-
After the component is mounted (if the dependency array is empty
[]) -
After every render (if no dependency array is provided)
-
After a dependency value changes (if the dependency array contains variables)
-
Cleanup Function Execution (Returned function from
useEffect)
The cleanup function is called before the effect is re-executed and when the component unmounts.