Skip to content
engineering workbench.by StudyPort Academy
All errors
runtime · DEBUG LIBRARY

A component renders twice in development

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

In development, React renders components an extra time, and it runs effects, then their cleanup, then the effects again. The extra pass is a check for impure rendering and missing cleanup. It is not a production double render, and it is not a bug by itself. You see it when a component is wrapped in <StrictMode>, which many toolchains, including the default React template, add around the root.

A console.log in the component body or in an effect therefore appears twice locally and once in a production build. A fetch without cleanup can also fire twice in development and leave a stale response able to set state.

Minimal reproduction

An effect that assumes it runs once

useEffect(() => {
  analytics.track("opened");
}, []);

In development this setup runs, cleans up, and runs again.

The fix

Do not remove to hide the double log. Make render a pure calculation of props and state, and return a cleanup from any effect that subscribes, times, or fetches. Ignore outdated fetch results in that cleanup.

To confirm the behavior, compare a development build with a production build: the extra render and extra effect setup disappear in production. If something still happens twice in production, look for a real second state update, not .

Cleanup that matches the setup

useEffect(() => {
  const id = subscribe(onChange);
  return () => unsubscribe(id);
}, []);

The extra development cycle unsubscribes before subscribing again.