React Strict Mode Double Rendering
Problem
If you're seeing your React components render twice in development, this is likely caused by React Strict Mode. This intentional behavior is designed to help identify potential issues in your application.
When using Strict Mode, React intentionally double-invokes certain methods including:
- Function component bodies
- Class component
render()methods useStateinitializersuseEffectcallbacks
This helps surface side effects that might cause bugs in your application.
Understanding Strict Mode
React Strict Mode is a development-only tool that helps you:
INFO
- Identify components with unsafe lifecycles
- Warn about legacy string ref API usage
- Detect unexpected side effects
- Detect legacy context API
Strict Mode does not affect production builds - the double rendering only occurs in development.
Solutions
1. Keep Strict Mode (Recommended)
The React team recommends keeping Strict Mode enabled as it helps create more robust applications. Here's how to handle the double-rendering behavior:
For API Calls in useEffect
If you have expensive API calls, use a ref to track whether the effect has already run:
import { useEffect, useRef } from 'react';
function MyComponent() {
const hasFetched = useRef(false);
useEffect(() => {
if (hasFetched.current) return;
hasFetched.current = true;
// Your API call here
fetchData();
}, []);
// Rest of your component
}For Console Log Cleanup
If double console logs are distracting, use React Developer Tools to hide them:
Enable "Hide logs during second render"
- Install React Developer Tools
- Open Chrome DevTools and go to the Components tab
- Click the gear icon → Debugging tab
- Check "Hide logs during second render in Strict Mode"
2. Disable Strict Mode (Not Recommended)
If you must disable Strict Mode:
For Create React App
Remove the <React.StrictMode> wrapper in src/index.js:
// Before
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// After
ReactDOM.render(
<App />,
document.getElementById('root')
);For Next.js
// next.config.js
module.exports = {
reactStrictMode: false,
}WARNING
Disabling Strict Mode removes valuable development-time checks that can prevent bugs in production.
Best Practices
- Design for idempotency - Ensure your components can handle being rendered multiple times without side effects
- Use useRef for tracking - Track initialization state rather than relying on render count
- Test in production - Remember that double rendering doesn't occur in production builds
- Embrace the checks - Use Strict Mode to identify and fix potential issues early
When Double Rendering Indicates Real Problems
If your component behaves differently after removing Strict Mode, this may indicate actual bugs:
- State that shouldn't be reset on render
- Side effects in render logic
- Incorrect dependency arrays in hooks
TIP
Strict Mode helps you find these issues before they affect your users in production.
Conclusion
React Strict Mode's double rendering is a valuable development tool, not a bug. Instead of disabling it, adapt your code to be resilient to multiple renders. This approach will result in more robust, production-ready React applications.
For most applications, the recommended approach is to keep Strict Mode enabled and ensure your components can handle the development-time double rendering.