parseAsString
Parserparamsync > parsers

parseAsString

Standard string parameter parser with automatic empty string purging and debounce support.

TSTypeScript Signature
IntelliSense
parseAsString: Parser<string | null>

What it does

Reads and writes query string values without altering character encoding or type casing. When paired with withDefault(""), any empty string input or matches to the default are omitted from the URL automatically according to the Clean URL Policy.

Best For

  • Free-text search inputs and live search bars
  • Active tab identifiers and category names
  • Slug-based selection filters
  • External referral or UTM campaign tracking parameters

How to use it properly

Import parseAsString from 'paramsync' and bind it to useQueryState:

TSExample.tsx
'use client';
 
import { useQueryState, parseAsString } from 'paramsync';
 
export function SearchFilter() {
// 300ms debounce ensures 0ms typing latency with debounced URL sync
const [search, setSearch] = useQueryState(
'q',
parseAsString.withDefault(''),
{ debounceMs: 300 }
);
 
return (
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search documentation..."
className="px-3 py-2 border rounded bg-zinc-900 border-zinc-800 text-zinc-100"
/>
);
}
main|paramsync
TypeScript JSXLn 22, Col 1
Advanced Technique

Functional Updates & Dynamic State

TSAdvancedUsage.tsx
// Functional state updates and clear button
<button onClick={() => setSearch('')}>Clear</button>
// Functional update:
setSearch((prev) => prev.trim().toLowerCase());
main|paramsync
TypeScript JSXLn 4, Col 1

Clean URL Behavior

Prevents ugly queries like `?search=` or `?tab=overview` from polluting browser address bars and analytics logs.

State ConditionClean URL ActionResulting URL
value === "" (empty string)Key removed from URLhttps://example.com/products (clean)
value === defaultValueKey removed from URLhttps://example.com/products (clean)
value === "mechanical keyboard"Serialized as query paramhttps://example.com/products?q=mechanical+keyboard

Configuration Parameters

ParameterTypeDefaultDescription
defaultValuestring""Optional default value supplied via .withDefault(fallback). Narrows return type to string.

Related API References