Skip to main content
Technology & EngineeringReact Patterns28 lines

React Router URL-Backed State

Use React Router search params as canonical filter or view state. Preserve sharing and back/forward behavior with stable encoding and no synchronization loops.

Quick Summary8 lines
Choose which state should survive a reload or shared link. Search text, selected categories and page often belong in the URL. Uncommitted input, local animation and sensitive fields usually should stay local. Define a schema that parses known keys, defaults, allowed values and repeated fields, preserving unrelated query keys where the page does not own them. Apply the same interpretation to loaders and rendered controls.

## Key Points

- [React Router useSearchParams](https://reactrouter.com/api/hooks/useSearchParams)
- [WHATWG URL and URLSearchParams](https://url.spec.whatwg.org/)
skilldb get react-patterns-skills/react-router-search-paramsFull skill: 28 lines
Paste into your CLAUDE.md or agent config

React Router URL-Backed State

Choose which state should survive a reload or shared link. Search text, selected categories and page often belong in the URL. Uncommitted input, local animation and sensitive fields usually should stay local. Define a schema that parses known keys, defaults, allowed values and repeated fields, preserving unrelated query keys where the page does not own them. Apply the same interpretation to loaders and rendered controls.

Parse once and write through navigation

Read the current location as the canonical applied state. Build a deterministic serializer with consistent defaults and ordering. Preserve intentional value order when it matters. Avoid changing the URL just to write defaults unless canonicalization is a real requirement. With React Router useSearchParams, treat the returned object as mutable but avoid changing it in place. Copy it, compute the complete next state once and pass that to setSearchParams. Multiple callback updates in the same tick do not provide React state queue semantics.

Pick push versus replace according to user intent. Applying a new filter set may create a useful history entry; typing every character usually should not. Reset pagination atomically with an applied filter change. If text is debounced, keep the local draft distinct and resynchronize it on an external location change so a pending old draft cannot overwrite Back. Avoid a pair of effects that continuously copy URL and component state in both directions.

Test browser history and invalid input

Load a deep link, verify filters and results, change a filter, copy the resulting URL, and open it in a clean context. Navigate Back and Forward and confirm both controls and data follow the restored location. Exercise invalid enums, repeated values, unknown keys and query values with spaces or punctuation. Test two changes in one interaction and confirm neither is lost. Ensure missing or invalid inputs fall back safely without throwing or emitting an infinite replacement loop.

Retain URLs with synthetic data, expected query state and observed navigation calls. Do not put account tokens or sensitive search strings in sample URLs. A parse/serialize unit test does not prove actual history, loader behavior or a browser restoration path.

References and rights

Original SkillDB instructions under LicenseRef-SkillDB-Content. References were checked 2026-11-13 and used for technical verification.

Install this skill directly: skilldb add react-patterns-skills

Get CLI access →