React Keys Bug — Input Focus Lost on List Reorder
Prepend an item and the text cursor jumps to the wrong input.
- 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
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.
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.
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.
- 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.
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).
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.
List Reordering Broke Input Focus: The Hidden Key Bug
- 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.
Key takeaways
Common mistakes to avoid
5 patternsUsing array index as a key prop
Mutating state directly instead of using setState
Omitting cleanup in useEffect for subscriptions and timers
Confusing props and state usage
Calling hooks conditionally or inside loops
Interview Questions on This Topic
What is the virtual DOM and how does it differ from the real DOM?
Frequently Asked Questions
That's JavaScript Interview. Mark it forged?
3 min read · try the examples if you haven't