Course contentsShow
← React
Lesson 1864 of 2,39643. RoutingFree lesson

Search Params for Component State: Tradeoffs

Understand when to use URL params for shareable state vs local state for ephemeral UI.

Search Params for Component State: Tradeoffs

You'll learn when to store UI state in the URL versus keeping it local to your component.

The Central Question

Not all state belongs in the URL. You've learned how to read and write search params, but now you must decide: should you? The tradeoff is between shareability and simplicity.

URL State: When to Use It

Store state in search params when:

  • Users should be able to share or bookmark the exact view: Filter settings, sort order, selected tab
  • The browser's back button should restore that state: Search queries, pagination
  • Other components on different routes might need the same state: Global filters that persist across navigation
// Good: Search and filters users want to share
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('q') || '';
const category = searchParams.get('category') || 'all';

Local State: When to Keep It Private

Use useState for ephemeral UI that's:

  • Temporary and not worth sharing: Dropdown open/closed, hover states, form validation errors
  • Too frequent to cause navigation noise: Live typing in an input before submission
  • Sensitive or user-specific: Partially filled forms, temporary selections
// Good: Transient UI that shouldn't persist
const [isModalOpen, setIsModalOpen] = useState(false);
const [draftText, setDraftText] = useState('');

The Mental Model

Think of search params as public state (visible in the address bar, part of the page's identity) and local state as private state (internal to the component's lifecycle).

Key Takeaway: Use search params for state that defines what the user is viewing and should be shareable; use local state for temporary UI interactions that shouldn't survive a page refresh.