{"success":true,"name":"design-url-state-with-searchparams-in-next-js-not-usestate","description":"Building filters, tabs, search, or pagination in a Next.js app","category":"ai-brimind","path":"ai-brimind/design-url-state-with-searchparams-in-next-js-not-usestate/SKILL.md","file":"SKILL.md","content":"---\nname: design-url-state-with-searchparams-in-next-js-not-usestate\ndescription: \"Building filters, tabs, search, or pagination in a Next.js app\"\n---\n\n# Design URL State With searchParams in Next.js, Not useState\n\n## When to use\nProject is Next.js, and you are about to store filters, page numbers, tabs, or a search query in useState only.\n\n## Example request\nfilters in useState → refresh or a shared link loses everything; the URL is the natural home for 'which view am I looking at'. — URL as state owner. | page number in state while the server fetches page data → the Server Component cannot see React state; it CAN read searchParams. — Server-readable state. | tab choice that nobody ever shares or reloads into → tiny throwaway UI state can stay local. — Ephemeral state is the exception. | back button does nothing after filtering → changes were invisible to browser history; router.push with params fixes it. — History integration for free.\n\n## Instructions\nAct like a designer who asks 'should this survive refresh and be shareable?' before choosing where state lives in Next.js. For every piece of UI state (filter, sort, page, tab, query): 1) Classify it: if a user would want to reload it, share it, bookmark it, or use the back button with it — it belongs in the URL as searchParams, not in useState. Only truly throwaway state (an open dropdown, a hover) stays local. 2) Use the platform pieces instead of custom plumbing: the page's searchParams prop on the server, useSearchParams and router.push/replace on the client, and <Link> with query objects. The server page can then fetch filtered data directly from the params — no client fetch dance needed. 3) Trace the full loop before coding: user changes filter → URL updates → server re-renders with new params → UI reflects URL. If any arrow in that loop is missing, name it. 4) Do not duplicate: never keep the same value in BOTH the URL and useState 'to be safe' — two owners drift apart; the URL is the single source, local state at most mirrors it while typing (debounced). 5) If unsure whether some state deserves the URL, ask the sharing question or check how similar pages (e-commerce filters, search pages) behave. Only implement once the owner is chosen, and explain why the URL (or local state) is the right owner for each piece — not just that filtering visually works.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #499\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}