AI summary by TwinMind
My Favorite State Manager Is...URLs?
Theo - t3․gg
6m 28s
State Management Problem Introduction
- State management = common hard problem in applications
- Developers make it harder by not using browser's best state manager: URL
- URL provides global state storage with additional benefits
Twitch Experience with URL State
- Speaker used URL for state extensively at Twitch
- Not in public-facing Twitch features, but internal admin panels
- Complex search queries for chat logs/reports stored in URL params
- Benefits achieved:
- Single global place for state storage
- Copy-paste URLs to share with team members
- Easier documentation and reproduction of specific page states
- URL bar = underappreciated part of React state management ecosystem
Next.js Commerce Example (Lee Robinson)
- Rebuilt with app router demonstrating URL state usage
- E-commerce page example:
- Color selection (black shirt) reflected in URL
- Size selection (small) reflected in URL
- Both parameters persist in URL for sharing
- Search functionality:
- Search term "mug" persisted as query=mug in URL
- Survives page refresh and navigation
- Filtering options also updated in URL
- Code implementation:
- Input takes default value from search params
- Uses useSearchParams hook in Next.js
- Form submission triggers function that:
- Gets value
- Creates new search params object
- Sets/deletes query parameters
- Navigates to new route with updated params
- Limitations noted:
- Not most type-safe solution
- Doesn't handle numbers well
- Missing key issue in example
TanStack Router Solution (Tanner Linsley)
- Current search param patterns in serious apps are "busted"
- Common problems:
- Render side effects
- Inconsistent inlined defaults
- Flat and zero dependencies
- Imperative/delayed configuration
- Manual restoration defaults and fallback
- TanStack Router solutions:
- Good type safety
- Declarative defaults (guarantees values)
- Schema-driven type safety
- Defines what page can accept in URL params
- Better serialization/deserialization
- Can serialize complex types (datetime)
- Tanner's focus: typesafe router with autocomplete
- Data loading = necessary evil, not primary interest
Decision Framework for URL vs Local State
- Key question: User behavior expectations
- Use URL state when:
- Want state to persist on refresh
- Want state to persist when sharing URL
- User expects continuity (e-commerce color selection)
- Use local state when:
- Refresh should reset content
- User doesn't expect persistence (calculator input)
- Use user expectations to guide state placement decisions
Benefits of Query Params for State
- Better server rendering patterns
- Reliable URL sharing
- True global access across web application
- Underrated part of modern web development
- Growing developer adoption (Lee Robinson, Tanner Linsley examples)
