Senior 3 min · March 06, 2026

React Keys Bug — Input Focus Lost on List Reorder

Prepend an item and the text cursor jumps to the wrong input.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • React renders UI declaratively: you describe state, React handles the DOM
  • Reconciliation diffs the virtual DOM in O(n) using heuristics (element type + key)
  • Hooks rules ensure consistent execution order across renders
  • Controlled components store form state in React, uncontrolled in DOM refs
  • Performance trap: inline arrow functions break memoisation via useCallback/React.memo
Plain-English First

Think of React like a smart whiteboard in a classroom. Instead of erasing and redrawing everything every time something changes, it only erases and redraws the parts that actually changed. That's React's whole superpower — it's incredibly efficient about what it updates on screen. The 'rules' React has (like hooks rules and component structure) are just the whiteboard's instructions for how to keep track of what changed and why.

React is the most popular front-end library in the world right now, and that means every JavaScript developer eventually walks into an interview room and faces React questions. The problem isn't that these questions are hard — it's that most developers know HOW React works but can't explain WHY it works that way. Interviewers can smell the difference in about 30 seconds. If you can only recite syntax, you'll get filtered out. If you can explain the reasoning behind design decisions, you get the offer.

React solves the messy problem of keeping your UI in sync with your data. Before React, developers manually poked at the DOM and prayed nothing got out of sync. React introduced a declarative model: you describe what the UI should look like given your current state, and React figures out the most efficient way to make it happen. It's the difference between telling someone 'make the button red' versus giving them step-by-step painting instructions every single time.

By the end of this article you'll be able to answer questions about the virtual DOM, reconciliation, hooks rules, controlled vs uncontrolled components, and performance optimization — and more importantly, you'll understand the reasoning deeply enough to handle follow-up questions you've never seen before. That's what actually passes interviews.

The Virtual DOM and Reconciliation: The Engine Under the Hood

The most common React interview question is 'What is the Virtual DOM?' but the follow-up 'How does reconciliation work?' is where most candidates stumble. React doesn't just refresh the page; it maintains a lightweight copy of the real DOM in memory. When state changes, React creates a new virtual tree and compares it (diffing) with the previous one.

Interviewers look for an understanding of the O(n) heuristic algorithm React uses. It assumes that two elements of different types will produce different trees and that a 'key' prop helps identify which elements are stable across renders. This prevents unnecessary re-renders and keep the UI snappy.

io/thecodeforge/react/ReconciliationDemo.jsxJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { useState } from 'react';

/**
 * TheCodeForgeReconciliation & Key Demo
 * This illustrates why 'keys' matter in the Virtual DOM.
 */
const ListManager = () => {
  const [items, setItems] = useState(['Docker', 'Kubernetes', 'Spring Boot']);

  const addTech = () => {
    // Adding to the start of the array to test React's diffing
    setItems(['React', ...items]);
  };

  return (
    <div className="p-4">
      <button 
        onClick={addTech} 
        className="bg-blue-500 text-white px-4 py-2 rounded"
      >
        Add React to Stack
      </button>
      <ul className="mt-4">
        {items.map((item, index) => (
          // BAD: Using index as key. React might lose state if items shift.
          // GOOD: Using the item string (or a unique ID) as the key.
          <li key={item} className="border-b py-1">
            {item}
          </li>
        ))}
      </ul>
    </div>
  );
};

export default ListManager;
Output
Component renders a button that prepends 'React' to a list, using unique keys for efficient DOM updates.
Forge Tip: Diffing Heuristics
In an interview, mention that while a full tree comparison is O(n³), React uses a heuristic O(n) approach. This shows you understand the engineering trade-offs behind the library's performance.
Production Insight
Using index as key in lists causes component state to misalign when items reorder.
Production debug: check keys via React DevTools or console.log each key in render.
Rule: keys must be unique and stable across renders — use item IDs, never indices.
Key Takeaway
Reconciliation uses keys to match virtual DOM nodes across renders.
Bad keys cause stale or wrong component state.
Rule: always use stable unique keys for dynamic lists.
Choosing the Right Key for Lists
IfItems have a unique 'id' field from API
UseUse key={item.id} — preferred, stable across renders
IfItems are static, never reordered or filtered
UseIndex key is acceptable but still risky — avoid if possible
IfItems are dynamic (insert/delete/sort)
UseMust use unique ID, never index

Hooks Rules: Why They Exist and What Breaks When You Break Them

React hooks enforce two rules: only call hooks at the top level, and only call them from React functions (components or custom hooks). The 'why' is critical for interviews. Under the hood, React relies on the order of hook calls between renders to maintain state and effect references. If you conditionally call a hook, the number of hooks changes between renders, and React's internal list of hook states gets misaligned — leading to bugs like stale state or skipped effects.

Mid-level developers can recite the rules; senior developers explain the linked-list based hook storage mechanism. React stores hooks for a component as a linked list where each hook's pointer depends on the call order. Conditional calls break the order, corrupting the list.

io/thecodeforge/react/HookRulesViolation.jsxJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React, { useState, useEffect } from 'react';

/**
 * TheCodeForgeHook Rules Violation Demo
 * This demonstrates what happens when you break the rules.
 */
const CounterWithCondition = ({ initial }) => {
  const [count, setCount] = useState(initial);
  // BAD: conditionally calling hook
  if (count > 5) {
    const [flag, setFlag] = useState(false); // WRONG — violates top-level rule
  }
  useEffect(() => {
    document.title = `Count: ${count}`;
  }, [count]);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(c => c + 1)}>Increment</button>
    </div>
  );
};

export default CounterWithCondition;
// Error: Hooks must not be called conditionally
Output
React throws an invariant violation: 'Hooks can only be called inside the body of a function component.'
Senior Interview Tip: Hook Storage as Linked List
Explain that React maintains a linked list of hook nodes per component. Each hook's next pointer depends on call order. Conditional calls break the chain, causing state mismatches.
Production Insight
Breaking hook rules usually surfaces as 'Rendered more hooks than during the previous render'.
Debugging: check for early returns before hook calls or conditional hooks inside components.
Rule: keep all hooks unconditional and at top level — no exceptions.
Key Takeaway
React stores hooks in order — a linked list tied to render number.
Conditional hooks break the order and corrupt internal state.
Rule: never call hooks inside conditions, loops, or nested functions.

Controlled vs Uncontrolled Components: The Production Reality

In interviews, you'll be asked the difference between controlled and uncontrolled components. Controlled components keep form state in React state (single source of truth). Uncontrolled components store form data in the DOM itself, and you access it via refs when needed (e.g., form submission). The choice matters for validation, real-time UI updates, and testability.

Most mid-level devs can define both. Senior devs discuss the tradeoffs: controlled components give you full control but cause a re-render on every keystroke (fine for most forms). Uncontrolled components are lighter but harder to react to changes. In production, you'll use controlled for most inputs, uncontrolled for file inputs or when you need to integrate with non-React code.

io/thecodeforge/react/ControlledVsUncontrolled.jsxJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, { useState, useRef } from 'react';

/**
 * TheCodeForgeControlled vs Uncontrolled Example
 */
const FormExample = () => {
  // Controlled input with state
  const [email, setEmail] = useState('');

  // Uncontrolled input with ref
  const passwordRef = useRef(null);

  const handleSubmit = (e) => {
    e.preventDefault();
    const password = passwordRef.current.value;
    console.log('Controlled email:', email);
    console.log('Uncontrolled password:', password);
  };

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label>Email (Controlled):</label>
        <input
          type="email"
          value={email}
          onChange={(e) => setEmail(e.target.value)}
        />
      </div>
      <div>
        <label>Password (Uncontrolled):</label>
        <input
          type="password"
          ref={passwordRef}
          defaultValue=""
        />
      </div>
      <button type="submit">Submit</button>
    </form>
  );
};

export default FormExample;
Output
A form with one controlled field (email) updating on keystroke, and one uncontrolled field (password) accessed via ref on submit.
Mental Model: State Ownership
  • Controlled: value + onChange === React is the sole source of truth.
  • Uncontrolled: defaultValue + ref === DOM holds the current value, React only reads when needed.
  • When you need to react to every change (e.g., live validation) → controlled.
  • When you only need the value at submit (e.g., password) → uncontrolled is simpler.
  • File inputs are always uncontrolled — you can't set the file path via state.
Production Insight
Switching a controlled input to uncontrolled mid-lifecycle causes React to warn and ignore future state updates.
Debug: check if you're mixing defaultValue and value props on the same input.
Rule: pick one pattern per input — don't switch between controlled and uncontrolled.
Key Takeaway
Controlled: React state drives the input value.
Uncontrolled: DOM stores the value, ref reads it.
Rule: controlled for validation/real-time; uncontrolled for simple reads or file inputs.

useEffect and Lifecycle: The Cleanup Trap

useEffect is the gateway to side effects in React components. It replaces componentDidMount, componentDidUpdate, and componentWillUnmount — but with a functional twist. The function passed to useEffect runs after every render by default, unless you specify a dependency array. The cleanup function returned from the effect runs before the effect re-runs and on unmount.

Interviewers probe for understanding of the dependency array and cleanup. Common pitfalls: missing dependencies causing stale closures, omitting cleanup leading to memory leaks (e.g., subscriptions or timers).

io/thecodeforge/react/useEffectCleanup.jsxJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React, { useState, useEffect } from 'react';

/**
 * TheCodeForge — useEffect with Cleanup
 * Tracks online/offline status using a browser event.
 */
const OnlineStatus = () => {
  const [isOnline, setIsOnline] = useState(navigator.onLine);

  useEffect(() => {
    const handleOnline = () => setIsOnline(true);
    const handleOffline = () => setIsOnline(false);

    window.addEventListener('online', handleOnline);
    window.addEventListener('offline', handleOffline);

    // Cleanup function: removes event listeners to prevent memory leak
    return () => {
      window.removeEventListener('online', handleOnline);
      window.removeEventListener('offline', handleOffline);
    };
  }, []); // Empty array: effect runs once (mount) and cleanup on unmount

  return (
    <div>
      <p>You are currently: {isOnline ? 'Online' : 'Offline'}</p>
    </div>
  );
};

export default OnlineStatus;
Output
Shows online status that updates when browser connectivity changes. Event listeners are properly cleaned up on unmount.
Common Cleanup Mistake
Missing cleanup in useEffect leads to memory leaks. Always return a cleanup function when you add subscriptions, timers, or event listeners.
Production Insight
Missing cleanup causes duplicate effect runs and stale connections.
Debug: use React DevTools to inspect effect instances or browser performance profiler for accumulating listeners.
Rule: if you subscribe in useEffect, return an unsubscribe/cleanup function.
Key Takeaway
useEffect merges mount, update, and unmount into one hook.
Always return cleanup for subscriptions — memory leaks are silent.
Rule: dependency array must list every reactive value used inside the effect.

Performance Optimisation: memo, useMemo, useCallback

React's reconciliation is fast, but unnecessary re-renders drag down performance in complex apps. The interview topic: when and how to prevent wasteful re-renders. React.memo wraps a component to skip re-render if props haven't changed (shallow comparison). useMemo memoises expensive computation results. useCallback memoises callback functions to maintain referential stability across renders.

Interviewers like to see that you understand when NOT to use these tools. Over-optimisation can make code harder to debug and even hurt performance by holding onto large memoised objects. The key is: only memoise when you've measured a re-render bottleneck.

io/thecodeforge/react/PerformanceOptimisation.jsxJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React, { useState, useMemo, useCallback } from 'react';

/**
 * TheCodeForgeMemoisation Patterns
 * Optimising a component that renders a large filtered list.
 */
const ExpensiveList = React.memo(({ items, onToggle }) => {
  console.log('ExpensiveList rendered');
  return (
    <ul>
      {items.map(item => (
        <li key={item.id} onClick={() => onToggle(item.id)}>
          {item.name}
        </li>
      ))}
    </ul>
  );
});

const App = () => {
  const [filter, setFilter] = useState('');
  const [items] = useState([/* large dataset */]);

  // useMemo: avoid recomputing filtered list on every render
  const filteredItems = useMemo(
    () => items.filter(item => item.name.includes(filter)),
    [items, filter]
  );

  // useCallback: stable reference for the callback so React.memo can work
  const handleToggle = useCallback((id) => {
    console.log('Toggled item', id);
  }, []);

  return (
    <div>
      <input
        placeholder="Filter"
        value={filter}
        onChange={(e) => setFilter(e.target.value)}
      />
      <ExpensiveList items={filteredItems} onToggle={handleToggle} />
    </div>
  );
};

export default App;
Output
Filter input updates state; memoised expensive list only re-renders when filtered items or callback reference changes.
Forge Tip: Profiling First
Never optimise prematurely. Use React DevTools Profiler to identify actual re-render hotspots before adding memo, useMemo, or useCallback.
Production Insight
Overusing useMemo can retain large arrays in memory longer than needed.
Debug: use browser performance tools to confirm memoisation reduces renders.
Rule: profile first, then memoise only the bottlenecked computations and callbacks.
Key Takeaway
React.memo stops re-renders when props haven't changed (shallow).
useMemo caches computed values; useCallback caches function references.
Rule: memoise only after profiling — premature optimisation wastes memory and confuses readers.
● Production incidentPOST-MORTEMseverity: high

List Reordering Broke Input Focus: The Hidden Key Bug

Symptom
When prepending an item to a list, the text cursor jumped to an unexpected input field. React preserved the component's state across re-renders but associated it with the wrong array element because the key (index) didn't uniquely identify the item.
Assumption
The team assumed any unique value, including index, would work fine for list keys since all items were distinct in content.
Root cause
Using array index as key tells React the first element is always the same component instance. When new item is inserted at index 0, React reuses the first component instance (which had the input state) and assigns it to the new data, causing the old input state to appear on the wrong element.
Fix
Replace key={index} with key={item.id} or another stable unique identifier. React then correctly associates component instances with data items regardless of position.
Key lesson
  • Never use array index as key if the list order can change (insert, delete, sort).
  • Stable ID-based keys preserve component state across re-renders.
  • React's reconciliation relies on keys to match previous component instances to current data.
Production debug guideQuick symptom-to-fix guide for the glitches that trip up mid-level devs4 entries
Symptom · 01
Component state resets unexpectedly on list reorder
Fix
Check that each list item has a stable key (unique ID). Swap key={index} to key={item.id} and verify re-renders preserve state.
Symptom · 02
useEffect runs more often than expected or misses dependencies
Fix
Review the dependency array. Use ESLint plugin react-hooks/exhaustive-deps. If an empty array is intended, double-check that no external values are used inside the effect.
Symptom · 03
Child component doesn't re-render after parent state change
Fix
Verify the child doesn't have React.memo incorrectly applied. Check that the prop passed is a new reference on each render (avoid inline objects/arrays). Use useMemo/useCallback if needed.
Symptom · 04
Form inputs don't update when typing
Fix
Check for controlled component pattern: value={state} onChange={(e) => setState(e.target.value)}. If using uncontrolled, ref might not trigger re-render.
★ Quick Debug Cheat Sheet — React Interview TopicsThree common React interview-related production bugs and the exact command or fix to apply.
List input states get mixed when items reorder
Immediate action
Stop typing and check list key prop usage.
Commands
console.log('key:', key) in render to see current keys
git grep 'key={index}' to find all index-based keys
Fix now
Replace key={index} with key={item.id} (or another stable unique value).
useEffect runs every render — infinite loop+
Immediate action
Check second argument to useEffect.
Commands
console.log('deps:', deps) to log dependency array each render
Check if state setter is inside the effect without deps
Fix now
Add proper dependency array: useEffect(() => {...}, [dep1, dep2]).
React.memo wrapped component still re-renders on parent update+
Immediate action
Verify props are referentially stable.
Commands
Use React DevTools Profiler to see why component re-rendered
console.log('props changed', newProps, prevProps) in custom comparison
Fix now
Wrap callback props with useCallback and objects/arrays with useMemo.
Feature Comparison
FeatureVirtual DOMReal DOM
Update SpeedBlazing fast (JavaScript objects)Slow (triggers browser layout/reflow)
Memory UsageLow (only in-memory objects)High (complex browser structures)
EfficiencyBatched updates via ReconciliationIndividual, manual manipulation
ConsistencyDeclarative (UI follows State)Imperative (UI must be manually poked)

Key takeaways

1
React's efficiency comes from the Virtual DOM, which avoids expensive browser reflows by batching updates.
2
Reconciliation is the process where React diffs two virtual trees to determine the minimum number of changes needed for the Real DOM.
3
Keys are not just for suppressing console warnings; they are essential for React to track item identity across re-renders.
4
Answering 'Why' (design decisions) is more valuable than answering 'How' (syntax) in a mid-to-senior level interview.
5
Hooks rely on call order
conditional calls break React's internal linked list of hook states.
6
Memoisation (React.memo, useMemo, useCallback) should be added after profiling, not preemptively.

Common mistakes to avoid

5 patterns
×

Using array index as a key prop

Symptom
Component state (e.g., input values, scroll position) gets misassigned when list items are reordered, added, or removed. React thinks the first item is still the same component instance.
Fix
Use a stable unique identifier from the data as key (e.g., item.id). Never rely on index if the list order changes.
×

Mutating state directly instead of using setState

Symptom
Component doesn't re-render after state change. React's shallow comparison doesn't detect mutation because the object reference stays the same.
Fix
Always use the setter returned by useState. For objects/arrays, create a new copy with spread or immutability helpers.
×

Omitting cleanup in useEffect for subscriptions and timers

Symptom
Memory leaks over time, duplicate side effects, or stale data updates after component unmounts (e.g., setState on unmounted component warning).
Fix
Return a cleanup function from the effect that removes event listeners, clears timers, or aborts pending requests.
×

Confusing props and state usage

Symptom
Attempts to modify props directly (props are readonly) or storing derived data from props in state unnecessarily, causing stale values or extra sync work.
Fix
Treat props as immutable data from parent. Use state only for data that changes over time within the component. Use useMemo for derived values from props.
×

Calling hooks conditionally or inside loops

Symptom
React throws 'Rendered more hooks than during the previous render' or 'Hooks can only be called inside the body of a function component' error.
Fix
Ensure all hook calls are unconditional and at the top level of the component. Move any conditional logic inside the hook (e.g., condition inside useEffect).
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01JUNIOR
What is the virtual DOM and how does it differ from the real DOM?
Q02SENIOR
Explain the Rules of Hooks. Why does React enforce calling hooks only at...
Q03SENIOR
What is the difference between controlled and uncontrolled components? W...
Q04SENIOR
How does React.memo differ from useMemo and useCallback?
Q05SENIOR
What happens if you use an empty dependency array in useEffect but acces...
Q01 of 05JUNIOR

What is the virtual DOM and how does it differ from the real DOM?

ANSWER
The virtual DOM is a lightweight JavaScript object representation of the real DOM. React uses it to minimize direct manipulation of the browser DOM, which is expensive. When state changes, React creates a new virtual tree, diffs it with the previous one (reconciliation), and then applies only the minimal set of changes to the real DOM. This makes updates faster and more predictable.
FAQ · 5 QUESTIONS

Frequently Asked Questions

01
What is the difference between a Controlled and Uncontrolled component?
02
How does React's 'useEffect' handle component lifecycle events?
03
What is 'lifting state up' in React?
04
What is the purpose of the key prop in lists?
05
When should you use useReducer instead of useState?
🔥

That's JavaScript Interview. Mark it forged?

3 min read · try the examples if you haven't

Previous
JavaScript Closures Interview Q
3 / 5 · JavaScript Interview
Next
Node.js Interview Questions