Performance: Route parameters
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 |
useParams() hook in React Router do?useParams()useParams().postId in React Router?: before the parameter name to define dynamic segments.<Route path="/post/:postId" element={<Post />} /> correctly uses /:postId. Options A, B, and D use incorrect syntax.<Route path="/profile/:username" element={<Profile />} /> and the URL /profile/jane_doe, what will useParams() return inside the Profile component?:username as a parameter. The URL /profile/jane_doe assigns "jane_doe" to this parameter.const params = useParams();
console.log(params.id);
<Route path="/product/:productId" element={<Product />} />. What is the problem?productId, but code accesses params.id.params.id is undefined here./user/42/post/7. Which route path and useParams() usage correctly extract both parameters inside the component?:userId and :postId to capture both values.{ userId, postId } is correct. Options A, B, and C misuse keys or destructuring.Pass the test and this pattern moves unknown