Conceptual Question

What are some common React anti-patterns?

React Ecosystem

What are some common React anti-patterns?

Interview answer

The most common React anti-patterns are mutating state, using unstable keys, defining components inside components, storing derived values in state, overusing effects, suppressing hook dependencies, premature memoization, and building components with too many responsibilities.

React

1. Mutating state in place

Problem: mutate an existing array, then pass the same reference to its setter.

JSX
// Problem
items.push(newItem);
setItems(items);
// Correction
setItems(previous => [...previous, newItem]);

Visible symptom: a list can appear unchanged, while other code observes unexpectedly modified shared data. Explanation: React compares state with Object.is; reusing a mutated reference can skip an update and corrupt previous snapshots. Trade-off: copying a shallow array does not clone its objects. Update each changed nested level; consider a reducer or immutable-update helper for complicated transitions.

2. Keys that track positions instead of items

Problem: an editable, reorderable list uses indices or freshly generated random keys.

JSX
// Problem
items.map((item, index) => <Row key={index} item={item} />);
// Correction
items.map(item => <Row key={item.id} item={item} />);

Visible symptom: after an insertion, a draft can appear on the wrong row; random keys remount rows and lose drafts. Explanation: keys identify siblings across renders. Index keys preserve the position's instance rather than the item's instance. Trade-off: indices can be acceptable for a truly static list without insertion, removal, reordering, or item-specific state. Prefer stable IDs for interactive data, and generate missing IDs when creating data rather than on every render.

3. Component definitions inside a render

Problem: a parent creates a new child component type each time it renders.

JSX
// Problem
function Editor() {
function Field() { return <input defaultValue="Draft" />; }
return <Field />;
}
// Correction
function Field({ value }) { return <input defaultValue={value} />; }
function StableEditor() { return <Field value="Draft" />; }

Visible symptom: a parent update wipes typing or focus. Explanation: the newly created function is a different type, so React replaces the old child instance. Trade-off: move the component to module scope and pass props. An ordinary function returning elements is a different case; avoid turning it into a newly defined component type.

4. Storing a value that can be derived

Problem: props and a second state variable represent the same fact.

JSX
// Problem
const [total, setTotal] = useState(0);
useEffect(() => setTotal(items.reduce((sum, item) => sum + item.price, 0)), [items]);
// Correction
const total = items.reduce((sum, item) => sum + item.price, 0);

Visible symptom: an old total appears during a commit, followed by another update; duplicated facts can drift. Explanation: the total belongs to the current render's inputs. Trade-off: a user-editable draft or a saved snapshot is independent state, rather than derived state. If this calculation is measurably expensive, consider memoization instead of using an effect to duplicate it.

5. Using an effect for a user action

Problem: a boolean drives a purchase effect instead of the click driving the request.

JSX
// Problem
useEffect(() => {
if (shouldBuy) void submitOrder();
}, [shouldBuy, submitOrder]);
// Correction
async function handleBuy() {
try { await submitOrder(); }
catch (error) { reportPurchaseFailure(error); }
}
// Attach handleBuy to the button's onClick.

Visible symptom: changing a function dependency or remounting can trigger an unintended order. Explanation: synchronization lifecycle is not the ownership boundary for a purchase interaction. Trade-off: effects are appropriate for ongoing external synchronization, such as subscriptions. A handler still needs pending-state protection and server-supported idempotency for purchases; moving code alone does not guarantee one order.

6. Hiding missing dependencies

Problem: an effect reads a prop but pretends it never changes.

JSX
// Problem: subscription never follows the new room.
useEffect(() => subscribe(roomId), []);
// Correction: subscribe must return an unsubscribe function.
useEffect(() => {
const unsubscribe = subscribe(roomId);
return unsubscribe;
}, [roomId]);

Visible symptom: the screen displays one room while receiving messages from another. Explanation: the old closure still owns the old subscription. Trade-off: fix the dependency or restructure code to remove the read. If a value truly belongs only to initialization, make that contract explicit. Do not silence the linter merely to force a desired execution count.

7. Memoizing without a demonstrated benefit

Problem: caching trivial work by default makes dependencies and code harder to inspect.

JSX
// Often unnecessary for a small list
const count = useMemo(() => items.length, [items]);
// Proportionate correction
const count = items.length;

Visible symptom: extra complexity with no useful improvement; an incorrect dependency can produce stale results. Explanation: memoization caches work or stabilizes references, and adds its own overhead. Trade-off: expensive calculations or a measured child-render bottleneck can justify memoization. Measure realistic interactions with the profiler, and account for compiler support in your project; do not declare memoization universally harmful or universally necessary.

8. Mixing unrelated responsibilities

Problem: fetching, validation, permission rules, dialog behavior, and table rendering all depend on one component's incidental state.

JSX
// Correction sketch: domain helpers are supplied by the application.
function UsersPage() {
const users = useUsers();
return <>
<UserTable view={users.view} />
<InviteForm onSubmit={users.invite} />
</>;
}

Visible symptom: a small form change breaks the table or makes isolated testing difficult. Explanation: unrelated concerns are coupled through shared ownership. Trade-off: extract coherent responsibilities, rather than splitting by an arbitrary file-length threshold. Keep state close to its consumers; lift only what must be shared. Props, composition, and context each have useful roles. Context is appropriate for shared cross-cutting data, but broad frequently changing provider values can increase consumer work. Avoid prescribing a new global store for every prop chain.

Walk through a code review

For each example, name the user-visible failure before proposing a correction. Ask what identity, state ownership, or synchronization invariant was broken, then choose a small change and explain its cost. The snippets are review excerpts: identifiers such as Row, subscribe, and useUsers represent application-specific contracts, rather than a standalone runnable application.

Interview follow-ups

  • Would this list remain correct after inserting an item at the start?
  • Which edit would preserve drafts, and which would intentionally reset them?
  • Is this value independent state or a calculation from existing inputs?
  • What measured bottleneck justifies the memoization or larger abstraction?

Reviewed 1 October 2026 against React: updating arrays in state, preserving and resetting state, you might not need an effect, and useMemo.

How to discuss an anti-pattern in a code review

Interviewers care less about memorizing a list than about explaining the failure mode and choosing the smallest safe refactor.

  • Name the broken invariant: identity, immutability, effect synchronization, or ownership.
  • Describe the visible symptom, such as stale UI, lost input state, extra renders, or hard-to-test code.
  • Offer the smallest correction before proposing a larger abstraction.
  • Measure before adding memoization; complexity is not a performance strategy.

Continue with nearby questions from the same topic cluster.