Updating Search Params Without Full Navigation
What you'll learn: How to modify query strings dynamically without triggering a full route navigation or component remount.
The Core Idea
You've learned to read query parameters with useSearchParams. Now you need to update them—perhaps when a user adjusts filters, changes pagination, or toggles a view mode. The setter function from useSearchParams lets you modify the query string while staying on the exact same route component, preserving your UI state.
How It Works
useSearchParams returns an array: [searchParams, setSearchParams]. The setter works like setState, but for the URL query string:
const [searchParams, setSearchParams] = useSearchParams();
// Update a single param
setSearchParams({ sort: 'price' });
// Merge with existing params
setSearchParams(prev => {
prev.set('page', '2');
return prev;
});
Think of it like updating state, except the "state" lives in the URL. React Router updates the browser's address bar without unmounting your component or triggering a loader refetch (unless you configure revalidation).
Why This Matters
Without this pattern, you'd be tempted to use navigate() with a manually constructed query string—messy and error-prone. setSearchParams handles encoding, preserving other params, and keeping navigation lightweight.
Common use cases:
- Filters: User clicks "In Stock" → update
?inStock=true - Pagination: Clicking "Next" → update
?page=3 - Search: Typing in a search box → update
?q=shoes
The component stays mounted, React just re-renders with the new params accessible via searchParams.get('page').
Key Takeaway:
Use the setSearchParams function to update query strings in place—no full navigation, no component remount, just a smooth URL update that keeps your UI responsive.