
Keep React App State in the URL After Refresh
Refresh sends your React app back to the home page? Your AI moves category, filters, page, sort, search and the open item into the URL, so reloads, bookmarks and shared links show the same screen. It brings a typed, dependency-free hook and covers the parts that usually break: junk params, resetting the page when filters change, Back closing a modal, and conflicts with React Router or Next.js. #Engenharia
Make a React single-page app remember where the user was: refresh, bookmark or share a link and land on the same filtered, paginated screen with the same item open.
What it does
Your AI reads your component (or your description), decides which state belongs in the URL, and wires it up:
- What goes in the URL and what stays out: filters, page, sort, search, view mode and the ID of the open item go in. Loading flags, toasts and form drafts stay out, and a modal's open state is derived from its ID.
- A typed schema with a default, parser and serializer for each param. Junk input such as
?page=-3&sort=hackfalls back safely. Defaults stay out of the URL, so the default screen has a clean address. - A ready-to-copy hook (
useUrlState.ts): no dependencies, TypeScript, with replace and push modes. It handles Back/Forward, keeps UTM and other params, keepshistory.stateintact, and keeps several components in sync. - The interactions that break in real apps: reset to page 1 when a filter changes, clamp the page when data shrinks, debounce search writes, make Back close a modal, and stop a visitor who arrived on a deep link from being sent off your site when they close the modal.
- Router adapters for React Router, Next.js (App and Pages Router) and TanStack Router, so it works with your router instead of fighting it.
- A 12-point test checklist plus round-trip unit tests for the schema.
Who it's for
React developers with list, catalog, dashboard or search screens who are tired of users losing their place on refresh, or who need shareable deep links.
How it works
- It checks your routing setup and asks which states should survive a refresh.
- It writes a param table: name, type, default, allowed values, and replace or push.
- It adds the hook (or a router adapter) and edits your component.
- It removes old one-off URL handling.
- It walks through the test checklist and runs your type check or build.
What you provide / what you get
You provide: your component code or repo, and your router if any.
You get: the param table, the hook file, the edited component(s) and the test steps.
Example requests
- "My product grid forgets the category and page on refresh. No router, Vite + TS."
- "Next.js App Router: put the dashboard's date range and tab into the URL so I can share links."
Requirements
A React project (hooks). Works with JavaScript or TypeScript. No new packages; if you already use a URL-state library, it uses that instead.
Limits
Client-side UI state only, not server data or form drafts. Not for secrets or personal data, since URLs end up in history and logs. Keep URLs under about 2,000 characters. For server-rendered pages, the server-side read is pointed out but not built.


