FrontendDeveloper.in

React question detail

When and how often does React invoke the setup and cleanup functions inside a useEffect hook?

  1. Setup Function Execution (useEffect)

The setup function (or the main function) you pass to useEffect runs at specific points:

  1. After the component is mounted (if the dependency array is empty [])

  2. After every render (if no dependency array is provided)

  3. After a dependency value changes (if the dependency array contains variables)

  4. Cleanup Function Execution (Returned function from useEffect)

The cleanup function is called before the effect is re-executed and when the component unmounts.

Back to all React questions
Get LinkedIn Premium at Rs 399