React traps · Chapter 8 of 16
Reading state right after setting it
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: State and rendering
- 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
The first press asked for the new code and then read the old one, an empty string, so nothing matched. That render stored the code; the second press read it. Nothing errors: the code is valid JavaScript doing exactly what it says.
React also batches a handler's updates: every setState in one event is applied together in a single render after the handler ends. Two setters, one render:
render: 0 ""
render: 4 "Thanks!"
So there is no moment inside the handler when the new value exists in the variable. Even flushSync, which makes React render straight away, does not change it:
after flushSync: 0 | page says: 4 stars
The page shows 4, the variable still says 0. The cure is plain JavaScript: work out the next value once, keep it in a local constant, give it to the setter and use the constant for everything else in the handler.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.jsx: turn a good coupon away on purpose
Write reproduce() in repro.jsx. Render CouponBox with offers that know DIWALI100, as { DIWALI100: { label: '₹100 off', off: 100 } }, and an onApply that does nothing. Type diwali100 in the box labelled Coupon, press the Apply button once, and resolve to the text of the message the box shows.
The check runs your reproduce() twice: beside the original CouponBox.jsx it must resolve to 'That code is not valid', and beside a fixed CouponBox.jsx it must resolve to '₹100 off applied'. So press Apply exactly once: a second press works even on the bug. No real waiting: reproduce() must finish within 5 seconds.
- Task 2
CouponBox.jsx: a coupon works on the first Apply
Fix CouponBox({ offers, onApply }) in CouponBox.jsx so one press of Apply checks the code in the box right now. The code is what was typed without spaces at the ends, in capital letters. When offers has that code, it shows the offer's label and applied (₹50 off applied) and calls onApply with the offer; otherwise it shows That code is not valid and calls onApply(null). It also shows the line Code and the code (Code FAMILY50).
onApply is called once for every press, and pressing Apply again with the same code gives the same answer. Keep the Coupon box, the Apply button, the messages and the Code line as they are. The check uses its own offers and types codes with small letters and spaces, so a fix that looks up exactly what was typed is as wrong as the bug.
- Task 3
CouponBox.test.jsx: a test that would have caught it
Write a vitest test file, CouponBox.test.jsx, that would have caught this bug before the launch. The check runs your file three times: beside the original CouponBox.jsx, where at least one test must fail; beside the fixed CouponBox.jsx, where every test must pass; and beside a wrong fix that looks up exactly what was typed, without trimming or capitals, where at least one test must fail.
So test it the way a customer does: type a good code in small letters (spaces are welcome), press Apply once, and expect its applied message and onApply called with the offer. A test of a code that is not valid is welcome too. No real waiting: each test must finish within 5 seconds.
What you will learn
- repro.jsx: turn a good coupon away on purpose
- CouponBox.jsx: a coupon works on the first Apply
- CouponBox.test.jsx: a test that would have caught it
More in State and rendering
Build "Reading state right after setting it" 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.