115Build a React scheduler demonstrating urgent updates versus startTransition() updates
Problem
Typing into a search box that filters a slow list freezes the input, because React renders the whole list for every keystroke before it can show the next character. Fix it with transitions.
Build <ProductSearch products={names} />:
- an
<input aria-label="Search products">whose value updates immediately on every keystroke (an urgent update); - a filtered list of
<SlowRow name={…} highlight={…} />items (provided: each one takes about 1 ms to render and highlights the match) that is updated in a transition; <p data-testid="count">{n} results</p>for the list being shown;- while the transition is pending, show
<p role="status">Updating…</p>and keep the previous list visible (slightly dimmed).
Use two pieces of state: query (urgent, drives the input) and listQuery (set inside startTransition, drives the list).
Try it yourself
Write your solution, run it, then submit to check it against the tests.
Step-by-step
query must update instantly because the user is looking at the input. The list can lag a little. Keep both: const [query, setQuery] = useState("") and const [listQuery, setListQuery] = useState("").
Unlock the full solution
What's inside
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Tests
Includes 2 code listings, 4 edge cases, 4 follow-up questions.
One-time payment by UPI, card or net banking. Launch price.