{"success":true,"name":"put-list-state-in-the-url-filters-search-and-page-belong-in-","description":"Building a Next.js list page with filters, search, or pagination held in useState","category":"ai-brimind","path":"ai-brimind/put-list-state-in-the-url-filters-search-and-page-belong-in-/SKILL.md","file":"SKILL.md","content":"---\nname: put-list-state-in-the-url-filters-search-and-page-belong-in-\ndescription: \"Building a Next.js list page with filters, search, or pagination held in useState\"\n---\n\n# Put List State in the URL: Filters, Search, and Page Belong in Query Params, Not Component Memory\n\n## When to use\nProject is Next.js/React, and you are building a screen whose view depends on user-chosen state — filters, search terms, current page, sort order, selected tab — and that state lives only in useState, so a refresh, a shared link, or the back button loses it.\n\n## Example request\nUser filters members to 'unpaid', clicks into one, presses back → the filter is gone and they scroll from the top again; state held only in useState dies on navigation, state in the URL (?status=unpaid) survives it — the URL is the oldest state manager on the web. An admin wants to send a colleague 'exactly this view' → with useState there is nothing to send; with ?search=ahmed&page=3 the link IS the view; shareability is a feature you get free by choosing where state lives. Refresh on page 7 of a long table → back to page 1, seven clicks lost; the page number was navigation state and belonged in the query string all along. Ephemeral UI like 'is this dropdown open' → stays in useState happily; the discipline sorts state into 'describes WHICH view' (URL) versus 'describes widget wiggle' (memory) — not everything moves.\n\n## Instructions\nAct as a frontend developer who treats the URL as the user's bookmark of truth — every list screen's answer to 'what am I looking at?' should be written in the address bar, because state that lives only in component memory evaporates on refresh, breaks the back button, and can never be shared, while state in the URL survives all three for free. When building any filtered, searched, sorted, or paginated view, do this: (1) Sort the screen's state into two buckets and say them: VIEW-DEFINING state (search term, active filters, sort, page number, selected tab — anything where two users with the same URL should see the same thing) goes in query params; WIDGET state (open dropdowns, hover, an input mid-typing before submit) stays in useState. If losing it on refresh would annoy the user, it is view-defining. (2) Use the framework's own mechanism instead of hand-parsing: in the Next.js App Router that is useSearchParams to read and router.push/replace with new params to write (searchParams arrive as props in server components — which pairs beautifully with your server-component discipline: the server can render the correct filtered list directly from the URL on first paint, no spinner). Updating params replaces improvised state syncing — the URL becomes the single source of truth and components just read it, which is your one-owner-per-data discipline applied to UI. (3) Wire interactions to write the URL, not parallel state: choosing a filter updates the query string (use replace for keystroke-level changes like live search so the history does not fill with every letter, push for deliberate view changes so back steps through meaningful views); the debounce discipline applies before the URL write for typed search. (4) Treat the params as border input like any other: they arrive as strings and anyone can edit them — parse page numbers with defaults, validate filter values against your enums, and let garbage degrade gracefully to the default view rather than crashing the render. (5) Verify with the three resurrection tests: refresh the page mid-filtered-view (same view returns?), copy the URL into a fresh tab (identical screen?), navigate away and press back (filters and page intact?). Only call the list page done when all three pass — a filter UI whose state dies on refresh is not managing state, it is renting it by the second.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #232\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}