Performance: Route parameters
MEDIUM IMPACT
Route parameters affect how quickly the app can match URLs and render the correct content, impacting initial load and navigation speed.
import { useParams } from 'react-router-dom'; function UserPage() { const { id } = useParams(); // Use memoization or lazy data fetching based on id return <div>User ID: {id}</div>; }
const routes = [ { path: '/user/:id', component: UserPage }, { path: '/user/:id/settings', component: UserSettings }, { path: '/user/:id/profile', component: UserProfile } ]; // Manually parsing params inside components with heavy computations
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual param parsing with heavy logic | Multiple DOM updates | Multiple reflows per route change | High paint cost due to re-renders | [X] Bad |
| useParams hook with memoized data | Minimal DOM updates | Single reflow per route change | Low paint cost | [OK] Good |