Skip to content
Design System
GitHub

Forms and lists in Next.js

Writes go through a Server Function; a list's query lives in the URL, so the server renders the right page first.

Forms

Start from the form example: React Hook Form and zod drawn with the kit's Field, installed with npx shadcn add @syncrobotic/form. MissionForm is a client component. Save through a Server Function that returns a refusal instead of throwing it, and turn it into an error in a small client file.

app/(app)/missions/new/actions.ts
"use server"

import type { MissionValues } from "@/components/form"

// A refusal is a return value: an error thrown here reaches the browser without its message in production.
export async function saveMission(values: MissionValues): Promise<{ error?: string }> {
  if (values.name.toLowerCase().includes("test")) return { error: "A mission named “test” already exists on this site." }
  // … write it to your API or database
  return {}
}
app/(app)/missions/new/new-mission.tsx
"use client"

import { useRouter } from "next/navigation"
import { MissionForm } from "@/components/form"
import { saveMission } from "./actions"

export function NewMission() {
  const router = useRouter()
  return (
    <MissionForm
      onSave={async (values) => {
        const { error } = await saveMission(values)
        if (error) throw new Error(error) // MissionForm shows it above the fields and keeps what was typed
        router.push("/missions")
      }}
    />
  )
}

The page (page.tsx) stays a Server Component that renders <NewMission />.

The form example covers the rest: errors after the first submit, focus on the first field in error, and a button that waits.

A list's query in the URL

useListQuery keeps page, limit, sort and search in the URL through an adapter. Its default writes window.history; in the App Router, pass one on the router so the query takes part in navigation. useSearchParams re-renders the component when the URL changes, so the adapter needs no subscribe. Wrap the list in <Suspense>, as useSearchParams asks.

hooks/use-router-list-query.ts
"use client"

import { usePathname, useRouter, useSearchParams } from "next/navigation"
import { type ListQueryAdapter, useListQuery } from "@/hooks/use-list-query"

// The URL goes through the router, so a list's query takes part in navigation and server rendering.
export function useRouterListQuery(options?: Parameters<typeof useListQuery>[0]) {
  const searchParams = useSearchParams()
  const router = useRouter()
  const pathname = usePathname()
  const adapter: ListQueryAdapter = {
    read: () => searchParams.toString(),
    write: (search, mode) => router[mode](search ? `${pathname}?${search}` : pathname, { scroll: false }),
  }
  return useListQuery({ ...options, adapter })
}
app/(app)/devices/page.tsx
import { parseListQuery } from "@/lib/list-query"

// The server reads the same query the browser will, so the first HTML is the right page.
export default async function DevicesPage({ searchParams }: { searchParams: Promise<Record<string, string | string[] | undefined>> }) {
  const query = parseListQuery(await searchParams, { sortable: ["name", "lastSeenAt"] })
  const page = await getDevices(query) // { items, total }: count with total, never items.length
  return <DevicesTable initial={page} />
}