Category: react
React Strict Mode and Double Rendering
Published on 27 Oct 2026
Explanation
In development, Strict Mode intentionally invokes render-related logic more than once to help expose impure rendering. This extra check does not happen in the same way in production builds.
Code:
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
createRoot(document.getElementById('root')).render(
<StrictMode><App /></StrictMode>
);
Explanation
With Strict Mode enabled, React performs an extra development-only effect setup and cleanup cycle to help reveal missing cleanup logic. Make effects safe to start and stop rather than trying to suppress the check.
Code:
useEffect(() => {
const connection = connect();
return () => connection.disconnect();
}, []);
Explanation
Seeing a render log twice in development can be expected under Strict Mode. Check whether the behavior also occurs in production and whether rendering or effects cause unwanted external side effects.
Code:
function Example() {
console.log('Render'); // May log twice in development Strict Mode
return <p>Hello</p>;
}
Explanation
Do not perform side effects during render, such as changing global variables, sending requests, or manually modifying the DOM. Rendering should calculate UI from props and state.
Code:
function Cart({ items }) {
// Good: pure calculation
const total = items.reduce((sum, item) => sum + item.price, 0);
return <p>Total: {total}</p>;
}
Explanation
A ref-based 'run once' guard can hide lifecycle bugs and prevent correct resynchronization when dependencies change. Implement the correct cleanup and dependency behavior instead.
Code:
useEffect(() => {
const subscription = subscribeToRoom(roomId);
return () => subscription.unsubscribe();
}, [roomId]);