Skip to article content
FrontendXLesson 02 / 40

Rendering, Reconciliation and Keys

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

beginner · about 50 minutes · 4 exercises

  • Predict
  • Explain
  • Debug
  • Interview

What you will be able to do

#
  • Distinguish the render phase from the commit phase.
  • Predict which components re-render after a state update.
  • Explain how React matches elements with keys during reconciliation.

Overview

#

Two phases

A React update has a render phase and a commit phase. During render, React calls your components to compute the next element tree. Rendering must be pure: no side effects, no mutation of values created outside the component. During commit, React applies the minimal DOM changes and runs effects and ref callbacks.

Rendering is not the same as updating the DOM

When a component re-renders, React does not automatically change the DOM. It compares the newly returned elements with the previous ones, a process called reconciliation, and only commits the differences. Re-rendering is usually cheap; the expensive part is unexpected DOM work and unstable identity.

The re-render cascade

Setting state schedules a re-render of that component and, by default, of its descendants. A parent re-rendering re-renders its children unless a child is wrapped in memo and its props are unchanged. This is why state should live as close as possible to where it is used: lifting state too high re-renders unrelated subtrees.

snippet.tsx
function Parent() {
  const [count, setCount] = useState(0);
  return (
    <>
      <button onClick={() => setCount((c) => c + 1)}>{count}</button>
      <ExpensiveChild />
    </>
  );
}

Every click re-renders ExpensiveChild even though it takes no props. Move the counter into its own component, or memoize the child, to avoid this.

Reconciliation and keys

React compares children of the same parent by position and key. With no key, React matches by index. With a key, React matches by identity. When an element's key changes, React unmounts the old subtree and mounts a new one, which resets local state and DOM state.

snippet.tsx
{tabs.map((tab) => (
  <Tab key={tab.id} label={tab.label} />
))}

Elements versus components

A component is a function. An element is the object it returns: { type, props, key }. React stores these objects as the fiber tree and uses them to diff. Understanding this distinction explains why defining a component inside another component is a bug: the inner function is a new type on every render, so React remounts the whole subtree.

snippet.tsx
// Bug: Inner is recreated each render and always remounts.
function Outer() {
  function Inner() {
    return <p>hello</p>;
  }
  return <Inner />;
}

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    How many times does Child render on the first click, and what is logged?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    function Child() {
      console.log("child");
      return <p>child</p>;
    }
    
    function Parent() {
      const [n, setN] = useState(0);
      console.log("parent");
      return (
        <>
          <button onClick={() => setN(n + 1)}>{n}</button>
          <Child />
        </>
      );
    }
    Choose the best answer
  2. Exercise 2Debug

    This list loses input focus and keeps stale text after a reorder. Identify the cause and fix it.

    starter.tsx
    items.map((item, index) => (
      <input key={index} defaultValue={item.label} />
    ))

    Reveal the reference answer
    reference.tsx
    items.map((item) => (
      <input key={item.id} defaultValue={item.label} />
    ))

    Authored expected output: After reordering, each input keeps its own value and focus because React matches rows by stable id.

    Index keys bind identity to position. When the array reorders, React matches the wrong DOM node to the wrong item and reuses stale DOM state. A stable item.id key lets React move nodes and preserve their state.

  3. Exercise 3Explain

    Explain why defining a component inside another component causes remounts, using the element type comparison.

    starter.tsx
    function Outer() {
      function Inner() { return <p>hi</p>; }
      return <Inner />;
    }

    Reveal the reference answer
    reference.tsx
    Each call to Outer creates a brand new Inner function. React compares element types by reference, so the new Inner type never equals the previous one, and React unmounts the old subtree and mounts a fresh one on every render.

    Authored expected output: A written explanation referencing function identity and element type equality.

    Reconciliation checks whether the element type at a position is the same. A recreated function has a new reference, so the check fails and the subtree remounts, discarding state and re-running effects.

  4. Exercise 4Interview

    In an interview, describe how you would locate an unnecessary re-render in a React tree and what you would do about it.

    starter.tsx
    // A dashboard where typing in a search box makes a chart flicker.

    Reveal the reference answer
    reference.tsx
    Use React DevTools Profiler to record an interaction and find which components re-rendered and why. If the chart re-renders because search state was lifted too high, move the state down into the search component. If the chart re-renders because a prop is a new object or function each render, stabilize it with useMemo or useCallback, or pass children instead. Confirm the fix with a second profile.

    Authored expected output: A structured answer: reproduce, profile, identify the re-render cause, apply the smallest fix, re-profile.

    Strong answers separate the symptom (flicker) from the cause (state placement or unstable identity), use the profiler as evidence, and verify the change rather than guessing.

Common misconceptions

#
  • A re-render always updates the DOM. Re-render only computes elements; the DOM changes only for differences.
  • React diffs by component name. React diffs by element type and key at the same position.
  • Defining a component inside another component is a harmless style choice. It creates a new type each render and remounts the subtree.
  • Rendering may perform side effects. Render must be pure; side effects belong in effects or event handlers.

Follow-up practice

#
  • react-state-and-updates
  • react-performance-and-identity

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.