useEffect(fn, []) runs fn…
Options
- After every render of the same component
- Once after mount, and cleans up on unmount (Correct option)
- Before the first render of the component
- Never, since an empty array disables effects
Answer
Once after the component mounts (cleanup on unmount); in development StrictMode it mounts, unmounts and re-runs once to surface bugs
Why
useEffect runs side effects (fetching data, subscriptions, timers) after React has drawn the component.
The dependency array says when to re-run it: [] = only after the first render; [userId] = whenever userId changes; no array = after every render.
The function you return is the cleanup (unsubscribe, clear timers). In development, StrictMode runs effects twice on purpose to reveal missing cleanups.
Remember
Dependency array controls when an effect re-runs.