Strict Mode's Double-Render Behavior
You'll learn why your components render twice in development mode and how this helps you write better code.
What is Strict Mode's Double-Render?
When you wrap your app in <React.StrictMode>, React intentionally calls your component functions twice during development (not production). This means your render phase runs twice for every single update.
This sounds wasteful, but it's actually a debugging superpower.
Why Does React Do This?
Remember that components must be pure functions (lesson 407)? They should produce the same output given the same props and state, with no side effects during render.
By rendering twice, React exposes impure behaviors:
- If you're mutating variables outside your component
- If you're calling random number generators or
Date.now()directly in render - If you have hidden side effects that create inconsistent UI
Analogy: Think of it like a science experiment's "control group." If running the same test twice gives different results, you know something's wrong with your procedure.
What You'll See
function MyComponent() {
console.log('Rendering!');
return <div>Hello</div>;
}
In development with Strict Mode, you'll see 'Rendering!' logged twice for each render. In production, it only runs once.
Don't Panic!
This is intentional and helpful. It doesn't mean your code is broken—it means React is helping you catch bugs before they reach production. If your component is pure (no side effects in render), the double-render won't cause problems.
Key Takeaway: Strict Mode's double-render behavior in development catches impure component bugs early by exposing side effects—it's a feature, not a bug, and only happens during development to help you write more reliable code.