Part 06 / Topic 6 of 7
StrictMode's double-invoke in development
In development only, <React.StrictMode> deliberately mounts a component, runs its effects, then immediately simulates an unmount (running every cleanup function) and a remount (running every effect again) - before anything the user would notice changes. This is not a bug, and it never happens in a production build; its entire purpose is to make Section 3's missing-cleanup mistake happen twice, immediately, loudly, during development, instead of once, quietly, in production, after real users have already mounted and unmounted the component hundreds of times. examples/05_strict_mode_double_invoke.jsx's SubscriptionWidget takes onSubscribe/onUnsubscribe callbacks so a test can watch this from the outside:
// examples/05_strict_mode_double_invoke.jsx
export function SubscriptionWidget({ onSubscribe, onUnsubscribe }) {
useEffect(() => {
onSubscribe();
return () => onUnsubscribe();
}, [onSubscribe, onUnsubscribe]);
return <p>Watching for price drops</p>;
}
Rendering <StrictMode><SubscriptionWidget .../></StrictMode> really calls onSubscribe twice and onUnsubscribe once (examples/05_strict_mode_double_invoke.test.jsx) - mount, cleanup, mount again, exactly as described above. Rendering the identical component with no StrictMode wrapper really calls onSubscribe once and onUnsubscribe not at all. A correctly-written effect (one with a cleanup function that actually undoes everything its setup did) behaves identically either way from the outside - the extra mount/cleanup/mount cycle is invisible to anyone except a test or a console.log watching for it. An effect without a correct cleanup function, by contrast, visibly misbehaves under StrictMode - a second subscription, a duplicate listener, a doubled timer - which is exactly the point: it surfaces the Section 3 mistake in development, before it ships.
Done here? The practice set for Part 6 — including the solutions — is on the part page.