Consider a Next.js component that updates the URL's searchParams when a user selects a filter. What happens when the user selects a new filter?
import { useSearchParams, useRouter } from 'next/navigation'; import { useState } from 'react'; export default function FilterComponent() { const searchParams = useSearchParams(); const router = useRouter(); const [filter, setFilter] = useState(searchParams.get('filter') || 'all'); function onChangeFilter(newFilter) { setFilter(newFilter); const params = new URLSearchParams(searchParams.toString()); params.set('filter', newFilter); router.replace(`?${params.toString()}`); } return ( <select value={filter} onChange={e => onChangeFilter(e.target.value)} aria-label="Select filter"> <option value="all">All</option> <option value="active">Active</option> <option value="completed">Completed</option> </select> ); }
Think about how router.replace works in Next.js App Router.
Using router.replace updates the URL without reloading the page. The component state updates with setFilter, so the UI and URL stay in sync.
Which code snippet correctly reads the 'page' parameter from the URL searchParams inside a Next.js client component?
Remember that useSearchParams returns a URLSearchParams object.
useSearchParams is the Next.js hook to access URL search parameters in client components. It returns a URLSearchParams instance, so you use .get() to read a parameter.
Given this code snippet inside a Next.js client component, why does the URL not update as expected?
const searchParams = useSearchParams();
const router = useRouter();
function updateCategory(cat) {
const params = searchParams;
params.set('category', cat);
router.replace(`?${params.toString()}`);
}Check if searchParams can be modified directly.
useSearchParams returns an immutable URLSearchParams object. You must create a new URLSearchParams instance to modify parameters. Directly calling set on the original does not change it.
In a Next.js client component, the state variable filter is initialized from useSearchParams().get('filter'). If the user manually changes the URL's filter parameter in the browser address bar, what happens to the filter state variable?
import { useSearchParams } from 'next/navigation'; import { useState } from 'react'; export default function Filter() { const searchParams = useSearchParams(); const [filter, setFilter] = useState(searchParams.get('filter') || 'all'); return <div>Current filter: {filter}</div>; }
Think about how React state works with initial values.
React state initialized from a value does not update automatically when that value changes externally. The filter state stays the same until explicitly updated or the component re-renders with new initial state.
What is the main advantage of syncing UI state with URL searchParams in a Next.js App Router application?
Consider how URLs help users share or return to specific views.
Using URL searchParams to store UI state means the URL reflects the current view. This lets users bookmark, share, or reload the page and keep the same state, improving user experience and navigation.