React · Junior · Question 26 of 75
When is the function returned from useEffect called?
- ✓Correct answer: Before the effect re-runs and when the component unmounts
- 2Right after the effect runs, in the same frame
- 3Only if an exception was thrown inside the effect
Explanation
The cleanup function undoes what the effect set up: it unsubscribes from events, clears timers, cancels requests. React calls it before the next run of the effect with new dependencies and when the component is removed.