Skip to content

QueryStateAdapterContext

const QueryStateAdapterContext: Context<
| AdapterValue
| null> = AdapterContext;

Defined in: tanstack/react/context.ts:45

The React context every query-state adapter provides (NuqsAdapter, QueryStateTestingAdapter) and that useQueryState / useQueryStates / useUrlSearchString read. Its value is an AdapterValue (the shared store plus adapter defaultOptions), or null outside an adapter, where the hooks throw.

Most apps never touch it. Read it to check whether an adapter is mounted, e.g. in a component that may also render outside the router. To write a new adapter, build the store with createQueryStateStore (pass a commit that writes to your router) and provide { store, defaultOptions } here; the store types (QueryStateStore, CommitFn, CommitInput, Snapshot, StoreUpdate) are exported from @xtandard/lib/tanstack too.

import { useContext } from "react";
import { QueryStateAdapterContext, useQueryState } from "@xtandard/lib/tanstack";
function UrlSearchBox() {
const [q, setQ] = useQueryState("q");
return <input value={q ?? ""} onChange={(e) => void setQ(e.target.value || null)} />;
}
function SearchBox() {
const hasAdapter = useContext(QueryStateAdapterContext) !== null;
return hasAdapter ? <UrlSearchBox /> : <input placeholder="Search" />;
}