Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

Why does an effect run twice in development, and how should cleanup work?

Questions in this chapter (6)
  • Draft
  • medium
  • Askedalmost always
  • Verified October 2026

Answer

Why twice. React may unmount and remount a component without the user noticing: navigating away and back, <Activity> hiding a tree and showing it again, saving the file in development. An effect that sets something up but never tears it down leaks a connection, a listener or a timer each time. To surface that early, Strict Mode in development runs every effect's setup → cleanup → setup when a component first mounts. State is kept, so only the effects (and, since React 19, ref callbacks) repeat. Production builds run setup once.

The right question isn't "how do I run it once?" but "does my effect survive a remount?" The test is that a user can't tell one setup from setup → cleanup → setup.

What cleanup does. Return a function that stops or undoes the setup: disconnect, remove the listener, clear the interval, destroy the widget, ignore or abort the request. React calls it:

  • before running the effect again because a dependency changed. It's the closure from the previous render, so it sees the previous values;
  • once when the component unmounts.

Within one commit, React runs every pending cleanup in the tree before any new setup.

Data fetching races. When query changes from r to re, two requests are in flight and the older one may answer last. Without cleanup, its setResults overwrites the newer result. Set an ignore flag in cleanup, or abort the request with an AbortController, so only the latest effect's response is applied. (Libraries and frameworks handle this, plus caching, for you.)

Don't use a ref to skip the second run. It hides the missing cleanup in development, and the leak still happens on real remounts. Work that must happen once per page load, not per mount, belongs outside components, at module level.

Example

function SearchResults({ query }) {
  const [results, setResults] = useState('');
  useEffect(() => {
    let ignore = false;
    fetch(`/search?q=${query}`)
      .then((response) => response.json())
      .then((json) => {
        console.log(json, ignore ? 'ignored' : 'applied');
        if (!ignore) setResults(json);
      });
    return () => { ignore = true; };
  }, [query]);
  return <p id="out">{results}</p>;
}

// Server: "r" answers after 300 ms, "re" after 100 ms
const root = createRoot(document.getElementById('root'));
root.render(<StrictMode><SearchResults query="r" /></StrictMode>);
setTimeout(() => root.render(<StrictMode><SearchResults query="re" /></StrictMode>), 50);
setTimeout(() => console.log('screen:', document.getElementById('out').textContent), 1000);
// → results for "re" applied
// → results for "r" ignored
// → results for "r" ignored
// → screen: results for "re"

Development build of React 19.3.1, Chrome 156 and Firefox 159, local Node server. Strict Mode fetched r twice; the first effect was cleaned up immediately. The production build logs one r line. Without the if (!ignore) check, the screen ended on results for "r" in both browsers.

Pitfalls

  • A hasRun ref or module flag to stop the double run. The effect then has no cleanup, and the bug ships.
  • Cleanup that doesn't mirror setup: removing a listener with a different function than the one added, or clearing a different timer.
  • Expecting cleanup to read the new props. It's the previous render's closure; that's what lets it disconnect from the old room.
  • Treating "fetched twice in development" as a production problem. It's one extra request in development; fix it with cleanup or a caching layer.

Follow-ups

What do ignore flags and AbortController differ in?
An ignore flag lets the request finish and drops its result. controller.abort() in cleanup cancels the request; fetch then rejects with an AbortError, which you must catch. Abort also frees the network.
Does wrapping only part of the app in <StrictMode> double its effects?
Not on the initial mount: React only does what can happen in production, and a child can't remount without its parent. Components mounted inside it later do get the extra cycle; both were confirmed in the test.
Are layout effects and insertion effects doubled too?
Layout effects yes, in the same cycle. useInsertionEffect didn't repeat in the test (React 19.3.1).

Deep dive

How it works

After the first commit of a tree inside <StrictMode>, React 19.3.1's development build walks the newly mounted fibers and, for each, disconnects its layout effects, refs and passive effects, then reconnects them (doubleInvokeEffectsOnFiber in ReactFiberWorkLoop.js; an implementation detail). Nothing re-renders, so state and DOM survive. For a component with a layout effect, an effect and a ref callback on its <div>, the development build logged, in Chrome 156 and Firefox 159:

  1. First mount: ref callback, layout effect, effect.
  2. Simulated unmount: layout cleanup, ref cleanup, effect cleanup.
  3. Simulated mount: ref callback, layout effect, effect.

On a normal update, passive effects run in two passes over the whole tree: every cleanup, then every setup.

function Item({ name, v }) {
  useEffect(() => {
    console.log(`setup ${name} v=${v}`);
    return () => console.log(`cleanup ${name} v=${v}`);
  }, [v]);
  return null;
}
// Parent (same effect) renders <Item name="A" /> and <Item name="B" />; v goes from 1 to 2:
// → cleanup A v=1
// → cleanup B v=1
// → cleanup Parent v=1
// → setup A v=2
// → setup B v=2
// → setup Parent v=2

Same in both browsers and builds. The cleanups carry v=1: they're the functions the previous render's effects returned.

Edge cases

  • Order on unmount is different. Unmounting the whole tree logged cleanup Parent before cleanup A, and Strict Mode's simulated unmount did the same. Updates ran children's cleanups first. These orders are observations, not documented guarantees; don't let cleanups depend on each other.
  • Abort in Strict Mode. With AbortController, the first r request was aborted by the simulated unmount and the second by the query change: two AbortError rejections, and the server saw both requests closed early.
  • Effects that can't be undone. Analytics "page view" sent twice in development is acceptable; it's development only. Don't disable Strict Mode for it.

Real-world scenario

A chat page connects in an effect. Without cleanup, development shows two "Connecting…" logs; in production the bug appears only after the user goes to Settings and back, and two connections receive every message. Returning () => connection.disconnect() fixes both, and the extra dev cycle becomes connect, disconnect, connect.

Trade-offs

The double cycle costs development time: duplicate requests, louder logs. In exchange, missing cleanup fails on the first mount instead of after a navigation pattern nobody tested. Fetching in effects also leaves caching, deduplication and races to you; a framework's data layer, or a cached promise read with use, avoids most of it.

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.