What is React.StrictMode double invocation?
Why Interviewers Ask This
This is a classic screening question for React.js roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
In React Strict Mode (development only), React intentionally invokes certain functions twice to help detect side effects in rendering. This includes: render functions, function component bodies, state initializers, and effect setup/cleanup pairs. The purpose is to surface bugs caused by components that are not "pure" — they produce different results on each invocation or have side effects during rendering. Since React's concurrent features may invoke render functions multiple times, your component body must be pure (no side effects during render). Effect double-invocation (React 18): effects also run twice in Strict Mode — mount → effect runs → cleanup runs → effect runs again. This detects effects that do not properly clean up. Example: if you open a WebSocket connection in useEffect but forget to close it in cleanup, you will see two connections in Strict Mode — revealing the bug. The double invocation behavior is ONLY in development and ONLY with Strict Mode. If your component behaves incorrectly with double invocations, it has a real bug that will manifest in production with concurrent rendering.
Pro Tip
This topic has React.js-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.