Beginner React Interview QuestionsBeginnerConcept
React · Question 28
What do the dependency array and cleanup function do in useEffect?
Direct answer
The dependency array controls when an Effect must resynchronize, and the optional cleanup function undoes the previous synchronization before the Effect runs again or the component is removed.
| Form | Typical behavior |
|---|---|
| useEffect(setup) | Runs after every committed render |
| useEffect(setup, []) | No reactive dependencies; setup is associated with mounting semantics |
| useEffect(setup, [a, b]) | Re-synchronizes when a or b changes by React's dependency comparison |
| return cleanup | Runs before re-synchronizing and when the component is removed |
Dependencies should include the reactive values used by the Effect. In development Strict Mode, React can run an extra setup-and-cleanup cycle to expose missing cleanup logic.