Skip to content
Guide/Search, debounce & pending state

Search, debounce & pending state

A search box is the hardest input to bind to a URL: it changes on every keystroke, and each change could be a navigation and a request. set() has two options for it.

src/articles/article-search.tsx
import type { QubeeListHandle } from '@qubeejs/react';
import type { articleList } from './article-list';
/** How long typing must pause before the URL changes. */
const SEARCH_DEBOUNCE_MS = 300;
/**
* The article search box. It shows the draft, so it never lags; it navigates
* 300 ms after the last keystroke, replacing the history entry; Clear goes out
* at once.
*/
export function ArticleSearch({ list }: { list: QubeeListHandle<typeof articleList> }) {
return (
<div role="search">
<label htmlFor="article-search">Search articles</label>
<input
id="article-search"
onChange={(event) =>
list.set({ q: event.target.value }, { debounce: SEARCH_DEBOUNCE_MS, replace: true })
}
type="search"
value={list.state.q ?? ''}
/>
{list.state.q !== undefined && (
<button onClick={() => list.set({ q: undefined })} type="button">
Clear
</button>
)}
<span aria-live="polite">{list.isPending ? 'Searching…' : ''}</span>
</div>
);
}

set(changes, { debounce: 300 }) updates state at once, and waits: the navigation happens 300 ms after the last call. Typing react is five calls and one navigation.

Changes to different params within the same wait are merged — each call builds on the draft the previous one left — so nothing typed is lost. A call without debounce goes out at once and takes any waiting change with it: that is why Clear never waits.

If the component unmounts while a change is waiting, the change is dropped. Nothing navigates after the component has gone.

{ replace: true } replaces the current history entry instead of adding one. With it, the back button leaves the search instead of stepping back through r, re, rea…

isPending is true while a debounced change is waiting, and while the navigation it started is under way. Navigations run inside startTransition, so with a router that renders on the server — the Next.js App Router — isPending stays true until the new page has arrived. Use it to dim the results, or to announce that a search is running:

<section aria-busy={list.isPending}>…</section>

A click on a link while a debounce is waiting commits the change at once. Without that, on a router that reports the new URL only after a transition, a search still waiting could fire after the user followed the link, and pull them back. See Routers that report the URL late.

The input’s value is state.q, and state is parsed back from the draft on every change. A param that trimmed would remove the space the user has just typed, and hello world could never be entered. That is why stringParam keeps a value exactly as typed by default, and why articleList trims in apply, where the query for the API is built:

apply: (builder, { q }) => {
const search = q?.trim();
// …
},

stringParam('q', { trim: true }) exists for params no input is bound to. Don’t use it for a live search box.

Going back is a URL change the hook did not cause, so it wins: any waiting change is dropped, and the box shows the query the address holds.

The Debounced search recipe includes the tests this box passes: one navigation per pause, Clear at once, and the space that stays.

On TanStack Router the router’s own search serialisers also have a say in what stays: see Keep the query as typed.