Skip to content
FrontDepth, Home

Back to the topic

Practice: Reconciliation and re-renders

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

4 predict-output · 3 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    This plain-JS model of React's default behaviour (no React involved) treats rendering a component as calling it, and a component renders the components it returns. What does it log on the first render, when Counter's state changes, and when App renders again with the same props?

    JavaScript
    const calls = [];
    
    function render(component, props) {
      calls.push(component.name);
      component(props);
    }
    
    function App({ theme }) {
      render(Header, { theme });
      render(Counter, { step: 1 });
      render(Footer, {});
    }
    function Header() {}
    function Counter() {
      render(Display, {});
    }
    function Display() {}
    function Footer() {}
    
    render(App, { theme: "light" });
    console.log(calls.join(" "));
    
    calls.length = 0;
    render(Counter, { step: 1 });
    console.log(calls.join(" "));
    
    calls.length = 0;
    render(App, { theme: "light" });
    console.log(calls.length);
  2. Open questionJunior

    What does "render" mean in React, and what causes a component to re-render?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    This model (no React involved) mimics React's bailout on identical elements: a slot is skipped when it receives the very same element object as last time. Layout gets Feed as children. What runs on the first render, when Layout's own state changes (update("root/0")), and when App's state changes (update("root"))?

    JavaScript
    const calls = [];
    const tree = new Map();
    const h = (type, props = {}) => ({ type, props });
    
    function render(path, element) {
      if (tree.get(path) === element) return;
      tree.set(path, element);
      update(path);
    }
    
    function update(path) {
      const { type, props } = tree.get(path);
      calls.push(type.name);
      type(props).forEach((child, i) => render(path + "/" + i, child));
    }
    
    function Sidebar() {
      return [];
    }
    function Post() {
      return [];
    }
    function Feed() {
      return [h(Post), h(Post)];
    }
    function Layout({ children }) {
      return [h(Sidebar), children];
    }
    function App() {
      return [h(Layout, { children: h(Feed) })];
    }
    
    render("root", h(App));
    console.log(calls.join(" "));
    
    calls.length = 0;
    update("root/0");
    console.log(calls.join(" "));
    
    calls.length = 0;
    update("root");
    console.log(calls.join(" "));
  4. Predict the outputMiddle

    This model of keyed reconciliation (no React involved) reuses a row when its key is found again, together with its draft — like the text in an uncontrolled input — and creates a row for a new key (marked *). The user typed notes for Milk and Bread, then Eggs is added at the top. What do index keys and id keys produce?

    JavaScript
    function reconcile(oldRows, items, keyOf) {
      const byKey = new Map(oldRows.map((row) => [row.key, row]));
      return items.map((item, index) => {
        const key = keyOf(item, index);
        const row = byKey.get(key) ?? { key, draft: "", created: true };
        row.label = item.name;
        return row;
      });
    }
    
    function show(rows) {
      return rows.map((row) => row.label + "=" + row.draft + (row.created ? "*" : "")).join(" | ");
    }
    
    const before = [
      { id: "a", name: "Milk" },
      { id: "b", name: "Bread" },
    ];
    const after = [{ id: "c", name: "Eggs" }, ...before];
    
    for (const keyOf of [(item, index) => index, (item) => item.id]) {
      const rows = reconcile([], before, keyOf);
      rows[0].draft = "2 l";
      rows[1].draft = "whole";
      for (const row of rows) row.created = false;
      console.log(show(reconcile(rows, after, keyOf)));
    }
  5. Open questionMiddle

    Why does React need keys in lists, and what goes wrong with index keys or random keys?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Predict the outputSenior

    In this model of batching (no React involved), setState queues an action and schedules a single flush in a microtask. What does it log?

    JavaScript
    let state = 0;
    let queue = [];
    let scheduled = false;
    let renders = 0;
    
    function setState(action) {
      queue.push(action);
      if (!scheduled) {
        scheduled = true;
        queueMicrotask(flush);
      }
    }
    
    function flush() {
      scheduled = false;
      for (const action of queue) state = typeof action === "function" ? action(state) : action;
      queue = [];
      renders += 1;
      console.log("render " + renders + ": " + state);
    }
    
    function handleClick() {
      const count = state;
      setState(count + 1);
      setState(count + 1);
      setState((c) => c + 1);
      console.log("in handler: " + state);
    }
    
    handleClick();
    setTimeout(() => {
      setState((c) => c + 10);
      setState(100);
      setState((c) => c * 2);
      console.log("in timeout: " + state);
    }, 0);
  7. Open questionSenior

    Typing into a search box makes a large React page lag. How do you find the cause and reduce the work?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.