React traps · Chapter 15 of 16

useCallback with a missing dependency

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Effects
  • 3 tasks
  • React
  • In the all labs plan

The first 2 chapters of React traps are free. This one comes with the all labs plan.

Why you need this

A memo component renders again only when its props change, and a function made during a render is a new function every time. So passing a fresh function to a memo child makes it render on every keystroke anyway. A memo child under a box, five letters typed:

a new function each render: Child rendered 6 times for 5 keystrokes
with useCallback: Child rendered 1 times for 5 keystrokes

That is the reason for Thursday's change, and it is a good one. The price is the list: it must name every value from the component that the function reads, props and state alike. Miss one and the function quietly uses an old value; nothing errors, and the page, which renders fresh, looks right. A lint rule, react-hooks/exhaustive-deps, catches this in most projects; the lab has no lint, so here it is your eyes.

Do not swap one dependency for another, either: a list that names the coupon but not the seats sends the right coupon with an old basket.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.jsx: lose the coupon on purpose

    Write reproduce() in repro.jsx. Render Checkout with one seat, [{ id: 'E7', price: 180 }], and an onPay that keeps the order it is given. Type diwali100 in the Coupon box, press Apply, then press the Pay button (its name starts with Pay), and resolve to the coupon in the order onPay was given.

    The check runs your reproduce() twice: beside the original Checkout.jsx it must resolve to '' (the page showed ₹100 off, Pay sent no coupon), and beside a fixed Checkout.jsx to 'DIWALI100'. So the coupon must be applied before Pay. No real waiting: reproduce() must finish within 5 seconds.

  2. Task 2

    Checkout.jsx: Pay sends what the page shows

    Fix Checkout({ seats, onPay }) in Checkout.jsx so Pay always sends what the page shows at that moment: it calls the onPay passed in now with { seats, coupon }, where seats are the ids of the seats passed in now, in order, and coupon is the coupon applied now (trimmed and in capitals, as Apply makes it), or '' when none was applied. The total stays as it is: each seat's price plus ₹20 a seat, ₹100 less with DIWALI100.

    Keep pay memoised with useCallback, so the PayBar does not render again while someone types, and keep the labels, buttons and lines as they are. The check uses its own seats, applies a coupon, then changes the seats from outside, then the seats, the coupon and onPay together, and presses Pay after each. A fix that remembers the coupon but forgets the seats is as wrong as the bug.

  3. Task 3

    Checkout.test.jsx: a test that would have caught it

    Write a vitest test file, Checkout.test.jsx, that would have caught this bug. The check runs your file three times: beside the original Checkout.jsx, where at least one test must fail; beside the fixed Checkout.jsx, where every test must pass; and beside a wrong fix that lists the coupon instead of the seats, where at least one test must fail.

    So test both things Pay sends, with an onPay made with vi.fn: apply a coupon, press Pay and expect the coupon in the order; and rerender Checkout with other seats, press Pay and expect the new seats. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: lose the coupon on purpose
  • Checkout.jsx: Pay sends what the page shows
  • Checkout.test.jsx: a test that would have caught it

More in Effects

Build "useCallback with a missing dependency" yourself

Parda's web app runs on React, and Rukhsana, its senior frontend engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of React traps are free. This one comes with the all labs plan.