typescriptintermediate
useSearchParamsState Hook
Syncs component state with URL search parameters for shareable filter/sort states.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useState, useCallback, useEffect } from 'react';
export function useSearchParamsState<T extends string>(key: string, defaultValue: T): [T, (value: T) => void] {
const [value, setValue] = useState<T>(() => {
if (typeof window === 'undefined') return defaultValue;
const params = new URLSearchParams(window.location.search);
return (params.get(key) as T) || defaultValue;
});
const setParam = useCallback((newValue: T) => {
setValue(newValue);
const url = new URL(window.location.href);
if (newValue === defaultValue) {
url.searchParams.delete(key);
} else {
url.searchParams.set(key, newValue);
}
window.history.replaceState({}, '', url.toString());
}, [key, defaultValue]);
useEffect(() => {
const handler = () => {
const params = new URLSearchParams(window.location.search);
setValue((params.get(key) as T) || defaultValue);
};
window.addEventListener('popstate', handler);
return () => window.removeEventListener('popstate', handler);
}, [key, defaultValue]);
return [value, setParam];
}Use Cases
- Filter state persistence
- Shareable search results
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Search Params in Server and Client Components
Handle URL search params in both server and client components with type-safe parsing.
Best for: product filters
#nextjs#search-params
typescriptintermediate
useFetch — Generic Data Fetching Hook
Custom React hook for data fetching with loading, error, and refetch states using the Fetch API.
Best for: API data fetching
#hooks#fetch
typescriptbeginner
useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
Best for: Search-as-you-type
#hooks#debounce
typescriptintermediate
useLocalStorage — Persistent State Hook
Sync React state with localStorage including SSR safety, JSON serialization, and cross-tab updates.
Best for: User preferences
#hooks#localstorage