React
frontendreactTroubleshootingIntermediate
Common React Mistakes and Anti-Patterns
Avoiding frequent React mistakes: mutating state directly, infinite useEffect loops, missing keys, stale closures, and over-using Context.
Verified: 2026-09-30
Tested on:react 19.x
Platforms:
web
#react#anti-patterns#mistakes#troubleshooting#debugging
Common React Mistakes and Anti-Patterns
A diagnostic checklist of the most common mistakes made in React codebases.
1. Mutating State Directly
// β WRONG: Mutating existing array
items.push("new item");
setItems(items); // React thinks reference didn't change!
// β
CORRECT: Return a new array reference
setItems([...items, "new item"]);2. Infinite useEffect Render Loops
// β WRONG: Updating state that is in the dependency array
const [data, setData] = useState([]);
useEffect(() => {
fetchData().then(res => setData(res)); // Triggers re-render -> triggers effect -> infinite loop!
}, [data]);
// β
CORRECT: Empty array or specific ID dependency
useEffect(() => {
fetchData().then(res => setData(res));
}, []); // Runs once on mount3. Stale Closures in Asynchronous Code
// β Bug: `count` is captured at the time setInterval was initialized (always 0)
useEffect(() => {
const id = setInterval(() => {
setCount(count + 1);
}, 1000);
return () => clearInterval(id);
}, []);
// β
Fix: Use the functional updater form
useEffect(() => {
const id = setInterval(() => {
setCount(prev => prev + 1); // Always gets latest value
}, 1000);
return () => clearInterval(id);
}, []);4. Derived State Stored in State
// β Anti-pattern: Redundant state sync
const [users, setUsers] = useState([]);
const [activeUsers, setActiveUsers] = useState([]);
// β
Fix: Derive directly during render!
const activeUsers = users.filter(u => u.isActive);Related Topics
React Performance Optimization Basics
Core React performance techniques: React 19 Compiler, React.memo, code splitting with React.lazy, virtualized lists, and avoiding wasted renders.
Introduction to Next.js App Router
Understanding Next.js: React Server Components, hybrid rendering, static site generation, streaming SSR, and edge architecture.