Skip to content

Keeping Typing Responsive

The Course
•
20 min study

This list is slow on purpose: each row takes about a millisecond to render, and up to 200 rows show at once. Type quickly in the search box.

A slow list React · 5 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import SlowList from "./SlowList.jsx"; import { topics } from "./topics.js"; export default function App() { const [query, setQuery] = useState(""); return ( <main> <h1>Find practice</h1> <div className="toolbar"> <input aria-label="Search practice" placeholder="Search practice" value={query} onChange={(e) => setQuery(e.target.value)} /> </div> <SlowList query={query} topics={topics} /> </main> ); } import { memo } from "react"; // Pretends each row is expensive to render, about 1 ms each. function SlowRow({ text }) { const start = performance.now(); while (performance.now() - start < 1) { // Busy on purpose. } return <li className="card">{text}</li>; } function SlowList({ query, topics }) { const needle = query.trim().toLowerCase(); const matches = topics.filter((topic) => topic.toLowerCase().includes(needle)).slice(0, 200); return ( <ul className="cards"> {matches.map((topic) => ( <SlowRow key={topic} text={topic} /> ))} </ul> ); } export default memo(SlowList); const words = ["Components", "Props", "State", "Reducers", "Context", "Refs", "Hooks", "Effects", "Memo", "Transitions"]; // 400 made-up study topics, to make the list slow to render. export const topics = Array.from({ length: 400 }, (_, i) => `${words[i % words.length]} practice ${i + 1}`); body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

The letters appear in bursts. Each keystroke changes query, and React re-renders the whole list before it can show the next letter. In our tests, each keystroke took around 200 ms to appear. Anything over about 100 ms feels broken.

Not all updates are equally urgent

The text in the box must update at once: it's what the user is doing. The list can lag a little behind. React lets you say so.

useDeferredValue

JavaScript
import { useDeferredValue, useState } from "react";
JavaScript
const [query, setQuery] = useState("");
const deferredQuery = useDeferredValue(query);
JSX
<SlowList query={deferredQuery} topics={topics} />

On each keystroke, React first re-renders with the old deferredQuery. SlowList is wrapped in memo, so with unchanged props it's skipped, and the new letter appears straight away. Then React renders again in the background with the new deferredQuery. If you type another letter first, it abandons that background render and starts over with the newest value:

JSX
function SlowList({ query, topics }) {
  const needle = query.trim().toLowerCase();
  const matches = topics.filter((topic) => topic.toLowerCase().includes(needle)).slice(0, 200);
  return (
    <ul className="cards">
      {matches.map((topic) => (
        <SlowRow key={topic} text={topic} />
      ))}
    </ul>
  );
}

export default memo(SlowList);

Make the change and type quickly again. The input keeps up; the list follows a moment later. In our tests, keystrokes went from about 200 ms to under 20 ms.

memo matters here. Without it, SlowList would re-render with the urgent update too, because its parent did, and nothing would improve.

useTransition

useDeferredValue defers a value. useTransition marks a state update as non-urgent, and tells you when it's in progress:

JavaScript
const [isPending, startTransition] = useTransition();

function selectTab(tab) {
  startTransition(() => {
    setTab(tab);
  });
}

Use it when you own the slow update, such as switching to a tab whose content is slow to render. isPending lets you show that something is happening, while the old tab stays usable. Here Practice takes about a third of a second to render:

A slow tab React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import SlowPractice from "./SlowPractice.jsx"; const tabs = ["Overview", "Practice", "Notes"]; export default function App() { const [tab, setTab] = useState("Overview"); function selectTab(next) { setTab(next); } return ( <main> <h1>Study hub</h1> <div className="toolbar"> {tabs.map((name) => ( <button key={name} aria-pressed={tab === name} onClick={() => selectTab(name)}> {name} </button> ))} </div> {tab === "Overview" && <p>Three topics to study this week.</p>} {tab === "Practice" && <SlowPractice />} {tab === "Notes" && <p>Review effects before Friday.</p>} </main> ); } // Pretends each row is expensive to render, about 1 ms each. function SlowRow({ text }) { const start = performance.now(); while (performance.now() - start < 1) { // Busy on purpose. } return <li className="card">{text}</li>; } export default function SlowPractice() { const rows = Array.from({ length: 300 }, (_, i) => `Practice question ${i + 1}`); return ( <ul className="cards"> {rows.map((row) => ( <SlowRow key={row} text={row} /> ))} </ul> ); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Click Practice, then Notes straight away. The page freezes: neither button responds until the whole practice list has rendered, and only then does Notes open.

Now make the switch a transition. Import useTransition, call it at the top of App, wrap setTab(next) in startTransition, and show that the switch is in progress next to the buttons:

JSX
{isPending && <span className="muted">Loading…</span>}
Check what you see

Click Practice: Loading… appears at once while Overview stays on screen, and a moment later the practice list replaces it. Click Practice then Notes quickly: Notes opens almost straight away. React abandoned the unfinished practice render, because a newer update arrived.

Deferring rendering doesn't debounce network requests and doesn't move your JavaScript to a worker. Keep the input's own state update urgent; pass the deferred value to the expensive view. For requests, Day 5's debouncing and stale-response handling still matter.

Show that the list is catching up

While the list lags behind, it shows results for an older query. Make that visible: when query !== deferredQuery, the list is stale.

Exercise

A stale indicator

While the list hasn't caught up, show Updating… next to the search box, and fade the list to half opacity.

Your working space React · 5 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import SlowList from "./SlowList.jsx"; import { topics } from "./topics.js"; export default function App() { const [query, setQuery] = useState(""); return ( <main> <h1>Find practice</h1> <div className="toolbar"> <input aria-label="Search practice" placeholder="Search practice" value={query} onChange={(e) => setQuery(e.target.value)} /> </div> <SlowList query={query} topics={topics} /> </main> ); } import { memo } from "react"; // Pretends each row is expensive to render, about 1 ms each. function SlowRow({ text }) { const start = performance.now(); while (performance.now() - start < 1) { // Busy on purpose. } return <li className="card">{text}</li>; } function SlowList({ query, topics }) { const needle = query.trim().toLowerCase(); const matches = topics.filter((topic) => topic.toLowerCase().includes(needle)).slice(0, 200); return ( <ul className="cards"> {matches.map((topic) => ( <SlowRow key={topic} text={topic} /> ))} </ul> ); } export default memo(SlowList); const words = ["Components", "Props", "State", "Reducers", "Context", "Refs", "Hooks", "Effects", "Memo", "Transitions"]; // 400 made-up study topics, to make the list slow to render. export const topics = Array.from({ length: 400 }, (_, i) => `${words[i % words.length]} practice ${i + 1}`); body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

Nothing new needs storing. The list is behind exactly when the value it shows differs from what's in the box, and both of those values are already available while App renders. Work out a true/false value from them, then use it twice.

Hint 2: a first step

Start with the text: add the deferred value as in the lab above, then show Updating… after the input when the two values differ. Type quickly and the word flickers on and off. For the fade, put the list inside a <div> and give it an inline style whose opacity depends on the same value.

Show one solution
JavaScript
const deferredQuery = useDeferredValue(query);
const isStale = query !== deferredQuery;
JSX
<div className="toolbar">
  <input aria-label="Search practice" placeholder="Search practice" value={query}
    onChange={(e) => setQuery(e.target.value)} />
  {isStale && <span className="muted">Updating…</span>}
</div>
<div style={{ opacity: isStale ? 0.5 : 1 }}>
  <SlowList query={deferredQuery} topics={topics} />
</div>

Was this article helpful?

Your response is saved on this device.