JavaScript traps · Chapter 14 of 16
An infinite re-render
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Time
- 3 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.
Why you need this
Now add a second subscriber that keeps the same numbers as an object instead of a string, and change the trips again:
subscribers.push((s) => {
set({ card: { rider: s.rider, trips: s.trips } });
});
try {
set({ trips: 5 });
} catch (error) {
console.log(error.name + ':', error.message);
}
RangeError: Maximum call stack size exceeded
=== compares objects by identity, not by what is inside:
console.log('4 trips' === '4 trips');
console.log({ trips: 4 } === { trips: 4 });
true
false
A fresh object is a change every time, so each call starts another, inside the last, until the call stack is full. Nothing is wrong with the store: the subscriber writes state while it is drawing.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: freeze the cart badge on purpose
Write reproduce() in repro.js. Make a store with createStore from cart-badge.js holding a cart with one item, for example { items: [{ name: 'masala chai', price: 40 }] }, and mount the badge on it with mountBadge(store, draw), where draw counts its calls. Then add a second item the way the Add button does, with the store's set and a new items list. If that throws, resolve to the error's name; if it does not, resolve to how many times draw ran for the added item (not counting the draw when the badge was mounted).
The check runs your reproduce() twice: beside the original cart-badge.js it must resolve to 'RangeError' (the badge drew itself until the stack ran out), and beside a fixed cart-badge.js to 1. So it must really mount the badge and add an item. Catch the error inside reproduce(); it must finish within 2 seconds.
- Task 2
cart-badge.js: one draw for each change
Fix mountBadge(store, draw) in cart-badge.js so the badge draws once for each real change. When it is mounted, it draws once, with the label '<count> in cart, ₹<total>' from cartSummary. When an item is added or taken out with the store's set, it draws once, with the new label. When set changes nothing (the same items list again), it does not draw at all. A later change still draws again.
Keep createStore and cartSummary as they are: other pages use them, and the checkout page calls cartSummary itself. The check uses its own carts and counts the draws: a fix that stops the loop but draws twice per change, or never draws again after the first time, is as wrong as the bug.
- Task 3
cart-badge.test.js: a test that would have caught it
Write a vitest test file, cart-badge.test.js, that would have caught this bug before Wednesday's release. The check runs your file three times: beside the original cart-badge.js, where at least one test must fail; beside the fixed cart-badge.js, where every test must pass; and beside a wrong fix that draws the badge once and never again, where at least one test must fail.
So mount the badge on a store, add an item and expect one draw with the new label, then add a second item and expect a second draw with its label. Each test must finish within 3 seconds.
What you will learn
- repro.js: freeze the cart badge on purpose
- cart-badge.js: one draw for each change
- cart-badge.test.js: a test that would have caught it
More in Time
Build "An infinite re-render" yourself
Tapri runs on JavaScript, and Kabir, its senior developer, 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 JavaScript traps are free. This one comes with the all labs plan.