home / skills / andrelandgraf / fullstackrecipes / using-nuqs

using-nuqs skill

/.agents/skills/using-nuqs

This skill helps you manage React state in URL query parameters with nuqs for shareable filters and deep-linkable dialogs.

npx playbooks add skill andrelandgraf/fullstackrecipes --skill using-nuqs

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.2 KB
---
name: using-nuqs
description: Manage React state in URL query parameters with nuqs. Covers Suspense boundaries, parsers, clearing state, and deep-linkable dialogs.
---

# Working with nuqs

Manage React state in URL query parameters with nuqs. Covers Suspense boundaries, parsers, clearing state, and deep-linkable dialogs.

## Implement Working with nuqs

Manage React state in URL query parameters with nuqs for shareable filters, search, and deep-linkable dialogs.

**See:**

- Resource: `using-nuqs` in Fullstack Recipes
- URL: https://fullstackrecipes.com/recipes/using-nuqs

---

### Suspense Boundary Pattern

nuqs uses `useSearchParams` behind the scenes, requiring a Suspense boundary. Wrap nuqs-using components with Suspense via a wrapper component to keep the boundary colocated:

```typescript
import { Suspense } from "react";

type SearchInputProps = {
  placeholder?: string;
};

// Public component with built-in Suspense
export function SearchInput(props: SearchInputProps) {
  return (
    <Suspense fallback={<input placeholder={props.placeholder} disabled />}>
      <SearchInputClient {...props} />
    </Suspense>
  );
}
```

```typescript
"use client";

import { useQueryState, parseAsString } from "nuqs";

// Internal client component that uses nuqs
function SearchInputClient({ placeholder = "Search..." }: SearchInputProps) {
  const [search, setSearch] = useQueryState("q", parseAsString.withDefault(""));

  return (
    <input
      value={search}
      onChange={(e) => setSearch(e.target.value || null)}
      placeholder={placeholder}
    />
  );
}
```

This pattern allows consuming components to use `SearchInput` without adding Suspense themselves.

### State to URL Query Params

Replace `useState` with `useQueryState` to sync state to the URL:

```typescript
"use client";

import {
  useQueryState,
  parseAsString,
  parseAsBoolean,
  parseAsArrayOf,
} from "nuqs";

// String state (search, filters)
const [search, setSearch] = useQueryState("q", parseAsString.withDefault(""));

// Boolean state (toggles)
const [showArchived, setShowArchived] = useQueryState(
  "archived",
  parseAsBoolean.withDefault(false),
);

// Array state (multi-select)
const [tags, setTags] = useQueryState(
  "tags",
  parseAsArrayOf(parseAsString).withDefault([]),
);
```

### Clear State

Set to `null` to remove from URL:

```typescript
// Clear single param
setSearch(null);

// Clear all filters
function clearFilters() {
  setSearch(null);
  setTags(null);
  setShowArchived(null);
}
```

When using `.withDefault()`, setting to `null` clears the URL param but returns the default value.

### Deep-Linkable Dialogs

Control dialog visibility with URL params for shareable links:

```typescript
import { Suspense } from "react";

type DeleteDialogProps = {
  onDelete: (id: string) => Promise<void>;
};

// Public component with built-in Suspense
export function DeleteDialog(props: DeleteDialogProps) {
  return (
    <Suspense fallback={null}>
      <DeleteDialogClient {...props} />
    </Suspense>
  );
}
```

```typescript
"use client";

import { useQueryState, parseAsString } from "nuqs";
import { AlertDialog, AlertDialogContent } from "@/components/ui/alert-dialog";

function DeleteDialogClient({ onDelete }: DeleteDialogProps) {
  const [deleteId, setDeleteId] = useQueryState("delete", parseAsString);

  async function handleDelete() {
    if (!deleteId) return;
    await onDelete(deleteId);
    setDeleteId(null);
  }

  return (
    <AlertDialog open={!!deleteId} onOpenChange={(open) => !open && setDeleteId(null)}>
      <AlertDialogContent>
        {/* Confirmation UI */}
        <Button onClick={handleDelete}>Delete</Button>
      </AlertDialogContent>
    </AlertDialog>
  );
}
```

Open the dialog programmatically:

```typescript
// Open delete dialog for specific item
setDeleteId("item-123");

// Deep link: /items?delete=item-123
```

### Opening Dialogs from Buttons

Use a trigger button to open the dialog:

```typescript
function ItemRow({ item }: { item: Item }) {
  const [, setDeleteId] = useQueryState("delete", parseAsString);

  return (
    <Button variant="ghost" onClick={() => setDeleteId(item.id)}>
      Delete
    </Button>
  );
}
```

---

## References

- [nuqs Documentation](https://nuqs.47ng.com/)
- [nuqs Parsers](https://nuqs.47ng.com/docs/parsers)

Overview

This skill shows how to manage React state in URL query parameters using nuqs for shareable filters, search inputs, toggles, and deep-linkable dialogs. It provides practical patterns for Suspense boundaries, parsing common types, clearing params, and controlling dialogs via the URL for reproducible links. The goal is predictable, bookmarkable UX with minimal wiring.

How this skill works

The skill replaces local useState with useQueryState from nuqs to keep state synced to the URL. It uses nuqs parsers (string, boolean, arrays) to convert between URL values and typed state and relies on a Suspense boundary because useSearchParams is used internally. Dialog visibility and other UI state become deep-linkable by storing identifiers or flags in query params and clearing them by setting the state to null.

When to use it

  • You want filters, searches, or toggles to be shareable via URL
  • You need deep-linkable dialogs or modals that open from a link
  • You want URL-driven state that survives reloads and can be bookmarked
  • You prefer zero-server routing changes and minimal client state plumbing
  • You need typed parsing for arrays, booleans, and strings in query params

Best practices

  • Wrap client components that use nuqs in a public Suspense wrapper so consumers don’t need to manage Suspense themselves
  • Use nuqs parsers (parseAsString, parseAsBoolean, parseAsArrayOf) with .withDefault() when sensible to provide predictable values
  • Clear URL params by setting the corresponding query state to null; with .withDefault() this removes the param but returns the default value in code
  • Keep query param names short and stable to avoid breaking saved links
  • Control dialogs via an id or flag param and reset it on close to keep URL and UI in sync

Example use cases

  • SearchInput component that syncs the q param for shareable searches and preserves value across reloads
  • Filter bar where tags, toggles, and search are synced to tags, archived, and q params for bookmarkable filter sets
  • Delete confirmation dialog opened by setting delete=<id> in the URL, allowing deep links to specific confirmations
  • Row actions that set a query param to open a modal for editing or viewing an item
  • Clear filters button that sets multiple query states to null to reset the URL

FAQ

Why do I need a Suspense boundary?

nuqs uses useSearchParams which can suspend; wrapping the client component in Suspense provides a safe fallback and keeps the boundary colocated with the component.

How do I remove a param from the URL?

Set the nuqs state to null. If you used .withDefault(), null clears the param from the URL but your code will receive the default value.