Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
It undoes what the effect set up, such as subscriptions, timers or listeners; React calls it before re-running the effect and when the component unmounts.
If an effect starts something that keeps running, like setInterval, an event listener or a WebSocket subscription, the effect should return a function that stops it. React runs that cleanup before the next run of the same effect, using the old values, and once more when the component is removed. Without cleanup you get duplicated listeners, timers that update unmounted components, and memory leaks. In development Strict Mode React deliberately mounts, cleans up and mounts again to reveal missing cleanups.