URL-based list filtering and pagination management
useListParams is a hook that synchronizes list page filtering, sorting, and pagination state with URL search parameters. It provides bookmarkable state management and type-safe form binding.
import { useListParams } from "@sonamu-kit/react-components";import { UserService } from "@/services/services.generated";import { UserListParams } from "@/services/sonamu.generated";export function UserListPage() { // Parse and initialize parameters from URL const { listParams, setListParams, register } = useListParams( UserListParams, { num: 24, page: 1, orderBy: "id-desc" as const, search: "id" as const, keyword: "", } ); // API call (pass listParams directly) const { data, isLoading } = UserService.useUsers("A", listParams); // ...}
Parameter Explanation:
UserListParams: Zod schema (auto-generated)
Default value object: Initial values when URL has no values
Why is a Zod schema needed?URL search parameters are strings, so a schema is needed to parse them into numbers or booleans.
UserListParams is auto-generated in sonamu.generated.ts and always stays in sync with backend type definitions.
{ value: listParams[name] ?? defaultValue[name] ?? (name === "page" ? 1 : ""), onValueChange: (value) => { if (name === "page") { // Preserve other filters on page change setListParams({ ...listParams, page: value }); } else { // Reset page to 1 on other filter changes setListParams({ ...listParams, page: 1, [name]: value === "" ? undefined : value }); } }}
Why auto page reset?:If a user changes the search keyword on page 3, there may be fewer results and page 3 may not exist.
Therefore, all filter changes except page automatically return to page 1.
The biggest advantage of useListParams is that filter state is stored in the URL.
Copy
# URL after user sets filtershttps://example.com/users?page=2&keyword=admin&orderBy=created_at-desc&role=admin# If you copy and share this URL or bookmark it# The same filters will be applied when accessed later
Benefits of bookmarkable state:
Users can bookmark frequently used filter combinations
Share URL to show colleagues the same view
Navigate through filter history with browser back/forward
Meaning of strict: falseuseSearch({ strict: false }) allows reading search parameters not defined in the current route.
This enables flexible handling of dynamically added filters.