Returns URL search parameters as a read-only URLSearchParams object. This provides the standard web API for working with query strings.
import { useSearchParams } from 'one'
// URL: /products?sort=price&category=electronics
export default function Products() { const searchParams = useSearchParams()
const sort = searchParams.get('sort') // 'price' const category = searchParams.get('category') // 'electronics' const missing = searchParams.get('missing') // null
return <Text>Sorted by {sort}</Text>}The returned object supports all standard URLSearchParams read methods:
const searchParams = useSearchParams()
// Get a single valuesearchParams.get('key') // string | null
// Check if param existssearchParams.has('key') // boolean
// Get all values for a key (for repeated params)searchParams.getAll('tag') // string[]
// Iterate over all entriesfor (const [key, value] of searchParams) { console.log(key, value)}
// Convert to stringsearchParams.toString() // 'key=value&other=123'Use getAll() when a parameter appears multiple times:
// URL: /filter?tag=react&tag=typescript&tag=native
const searchParams = useSearchParams()const tags = searchParams.getAll('tag')// tags = ['react', 'typescript', 'native']By default, useSearchParams only updates when the current route is focused. Pass { global: true } to update on any route change:
// Updates even when this screen is not focusedconst searchParams = useSearchParams({ global: true })global option cannot change between renders.The returned URLSearchParams is read-only. Calling set(), append(), or delete() will throw an error:
const searchParams = useSearchParams()
// ❌ These will throwsearchParams.set('key', 'value')searchParams.append('key', 'value')searchParams.delete('key')To update search params, use the router:
import { useRouter, useSearchParams } from 'one'
function Filters() { const router = useRouter() const searchParams = useSearchParams()
const updateSort = (sort: string) => { // Build new search string const params = new URLSearchParams(searchParams) params.set('sort', sort)
router.push(`/products?${params.toString()}`) }}| Hook | Returns | Use for |
|---|---|---|
useParams | Plain object | Quick access to params as object |
useSearchParams | URLSearchParams | Standard web API, repeated params |
Both hooks include path params and query params. Choose based on which API you prefer:
// Object styleconst { sort, page } = useParams()
// URLSearchParams styleconst searchParams = useSearchParams()const sort = searchParams.get('sort')const page = searchParams.get('page')Edit this page on GitHub.