Read route params and search params
Read a value from the path
A dynamic segment begins with a colon in a route pattern. The segment name becomes a key in the object returned by useParams. A path parameter identifies a resource or view, while a query parameter commonly describes an optional filter or sort order. Both are input from the URL; neither proves that a record exists or a user has permission. This lesson uses a fixed local dataset, so no remote fetch or credential is needed.
import React from 'react';import { MemoryRouter, Routes, Route, useParams, Link } from 'react-router';function findTrail(id) { return [{id:'oak', name:'Oak loop'}, {id:'lake', name:'Lake path'}].find(trail => trail.id === id);}function Detail() { const { trailId } = useParams(); const trail = findTrail(trailId); return <main><h1>{trail ? trail.name : 'Trail not found'}</h1><Link to="/trails/lake">Lake path</Link></main>;}export default function App() { return <MemoryRouter initialEntries={['/trails/oak']}><Routes> <Route path="/trails/:trailId" element={<Detail />} /> </Routes></MemoryRouter>;}At /trails/oak the heading is Oak loop; the same route pattern matches /trails/lake with a different value. At /trails/missing the pattern still matches, but the lookup fails and the component displays a missing-record message. Route params are strings when present; optional or unmatched values may be absent. Validate numeric values explicitly if you need numbers, and prefer matching known IDs over treating arbitrary URL text as a trusted resource path.
Use query parameters for optional filters
import React from 'react';import { MemoryRouter, Routes, Route, useSearchParams } from 'react-router';function normalizeLevel(value) { return value === 'easy' || value === 'moderate' ? value : 'all';}function Catalog() { const [params, setParams] = useSearchParams(); const level = normalizeLevel(params.get('level')); const trails = [{id:'oak',name:'Oak loop',level:'easy'}, {id:'lake',name:'Lake path',level:'moderate'}]; const visible = trails.filter(t => level === 'all' || t.level === level); function changeLevel(event) { const next = new URLSearchParams(params); if (event.target.value === 'all') next.delete('level'); else next.set('level', event.target.value); setParams(next); } return <main><h1>Trail catalog</h1><label>Difficulty <select value={level} onChange={changeLevel}> <option value="all">All</option><option value="easy">Easy</option><option value="moderate">Moderate</option> </select></label><ul>{visible.map(t => <li key={t.id}>{t.name}</li>)}</ul></main>;}export default function App() { return <MemoryRouter initialEntries={['/trails?level=easy']}><Routes> <Route path="/trails" element={<Catalog />} /> </Routes></MemoryRouter>;}The initial list should contain only Oak loop. Selecting All removes level, and choosing Moderate displays Lake path. get returns the first value or null; getAll returns repeated values. URLSearchParams handles query encoding. The URL is the single source of truth for this filter, so a separate synchronized state variable is unnecessary. Updating through setParams triggers navigation. Clone before editing to avoid mutating the stable but mutable params object without updating the location. Multiple setter calls in one tick do not have React state updater queue semantics.
Preserve unrelated query values
import React from 'react';import { MemoryRouter, Routes, Route, useSearchParams } from 'react-router';function withoutLevel(search) { const next = new URLSearchParams(search); next.delete('level'); return next;}function Filters() { const [params, setParams] = useSearchParams(); return <main><h1>Current filters</h1><p>Level: {params.get('level') ?? 'all'}</p> <p>Region: {params.get('region') ?? 'all'}</p> <button onClick={() => setParams(withoutLevel(params), {replace:true})}>Reset difficulty</button> </main>;}export default function App() { return <MemoryRouter initialEntries={['/trails?level=easy®ion=north']}><Routes> <Route path="/trails" element={<Filters />} /> </Routes></MemoryRouter>;}Reset should change Level to all but leave Region as north. This example replaces the current entry so the reset does not add another history step. Choose push or replace according to what the user expects Back to do. Do not include passwords, tokens or private data in URLs: they may appear in logs and history. React safely renders ordinary text here; do not use URL values as HTML or arbitrary redirect destinations.
Practice
Add a region filter that preserves difficulty. Test no query, unknown values, repeated level values, and a region containing a space. Explain what you would validate on the server if this became an authenticated application.