Skip to Content
TelamonJS

useUrlSync

Sync state with URL query params in the Next.js App Router

Hook to synchronize state with URL query params (Next.js App Router). Use it for state that should survive a reload or a shared link: active tab, open dialog, filters, pagination.

'use client'

import { useUrlSync } from '@telamonjs/ui'

const { getParam, setParam } = useUrlSync()

const status = getParam('status') // string | null
setParam('status', 'active') // pushes ?status=active
setParam('status', null) // removes the param

API

Read

MethodSignatureDescription
getParam(key: string) => string | nullRead a query param
getNumberParam(key: string, defaultValue?: number) => number | undefinedRead a numeric param with fallback
hasParam(key: string) => booleanCheck if a param exists
getAllParams() => Record<string, string>All params as an object

Write

MethodSignatureDescription
setParam(key, value, options?: { replace?: boolean }) => voidSet one param (null/undefined/'' deletes it)
setParams(updates: Record<string, value>, options?: { replace?: boolean }) => voidAtomic multi-param update in a single navigation
clearParam(key: string) => voidRemove one param
clearAllParams() => voidRemove all params

All writes use router.push(..., { scroll: false }) by default.


The replace Option

setParam and setParams accept { replace: true } to use router.replace instead of push — no browser history entry is created. This is the base of dialog deep-linking (dialogs.*.urlParam in CrudConfig).

// No history entry — good for transient dialog state
setParam('view', item.id, { replace: true })

// Atomic view → edit handoff: delete ?view and set ?edit in ONE navigation,
// avoiding stale intermediate snapshots
setParams({ view: null, edit: item.id }, { replace: true })

When to Use

  • State that must survive reload / link sharing (active tab, open dialog, filters)
  • Prefer useTabUrlSync for tab state — it wraps this hook with validation and history semantics
  • For a ListPage, prefer its urlSync prop: it keeps page, page size, sort and search in the query params, both ways

On this page