Diagnose impure rendering in StrictMode
Why repeated calculations must be harmless
A render should calculate UI from its inputs without changing pre-existing data or causing outside actions. React can repeat or abandon a calculation. Development StrictMode deliberately repeats certain work to expose bugs, including impure render functions and missing cleanup; its extra checks are not a production render-count contract. Treat state and props as snapshots. Never send a request, write shared data or trigger a notification merely because a component was evaluated.
function Shelf({ books }) { books.sort((a, b) => a.title.localeCompare(b.title, 'en')); return <ul>{books.map(book => <li key={book.id}>{book.title}</li>)}</ul>;}const books = [{ id: 'b', title: 'Botany' }, { id: 'a', title: 'Algebra' }];export default function App() { return <main><h1>Reading shelf</h1><Shelf books={books} /></main>;}The display looks sorted, but sort mutates the input array. A different component or event using the same array can now observe an unintended change. StrictMode does not guarantee every mutation will produce a warning or a visible duplicate; this example demonstrates an incorrect data contract even when it looks fine. Do not execute it against shared application data.
function sortedBooks(books) { return [...books].sort((a, b) => a.title.localeCompare(b.title, 'en'));}function Shelf({ books }) { return <ul>{sortedBooks(books).map(book => <li key={book.id}>{book.title}</li>)}</ul>;}const books = [{ id: 'b', title: 'Botany' }, { id: 'a', title: 'Algebra' }];export default function App() { return <main><h1>Reading shelf</h1><Shelf books={books} /></main>;}The displayed order is Algebra, Botany while the original array still begins with Botany. A local array copy may be sorted because it belongs to this calculation. Its objects are still shared, so changing a book title would require an appropriate object copy as well. This makes repeated calls produce equivalent results for the same inputs.
Move interaction-driven updates out of render
import { StrictMode, useState } from 'react';function DeskCounter() { const [count, setCount] = useState(0); return <section><h2>Desk reservations</h2><p>{count}</p> <button type="button" onClick={() => setCount(n => n + 1)}>Add</button> <button type="button" onClick={() => setCount(0)}>Reset</button></section>;}export default function App() { return <main><h1>Reading room</h1><StrictMode><DeskCounter /></StrictMode></main>;}The expected screen starts at 0, Add increments once per activation, and Reset returns to 0. The setter is invoked by a callback, not while evaluating JSX. Writing onClick={setCount(0)} would call the setter during render instead of passing a callback and can cause repeated rendering or a too-many-re-renders error. There are narrow documented patterns for guarded render-time state adjustment, but this ordinary reset is not one of them. StrictMode is a diagnostic tool: fix the code, do not disable the checks to hide a bug. For a full-app check the root entry can wrap App in StrictMode; effect and ref cleanup details depend on placement and version and are addressed later.
Practice
Explain what would happen if sortedBooks wrote directly to its argument. Then test a frozen source array using a plain JavaScript harness. Compare expected user-facing behavior in development and production without asserting a fixed render count.