Performance: Creating routes
This affects the initial page load speed and navigation responsiveness by determining how routes are matched and components are loaded.
import { lazy, Suspense } from 'react'; const Home = lazy(() => import('./routes/Home')); const About = lazy(() => import('./routes/About')); export const routes = [ { path: '/', element: <Suspense fallback={<div>Loading...</div>}><Home /></Suspense> }, { path: '/about', element: <Suspense fallback={<div>Loading...</div>}><About /></Suspense> } ];
import Home from './routes/Home'; import About from './routes/About'; export const routes = [ { path: '/', element: <Home /> }, { path: '/about', element: <About /> } ];
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager loading all routes | Low (few nodes initially) | 1 reflow on load | High paint cost due to large bundle | [X] Bad |
| Lazy loading route components | Low (few nodes initially) | 1 reflow on route change | Lower paint cost on initial load | [OK] Good |
/about?app/routes folder./about, create about.jsx inside app/routes.about.jsx inside the app/routes folder. -> Option A[param] in file names to define dynamic route parts.user/[id].jsx.user/[id].jsx inside app/routes. -> Option Bapp/routes/blog/[slug].jsx exporting a React component that renders <h1>{params.slug}</h1>, what will be rendered when visiting /blog/hello-world?[slug].jsx captures the URL part after /blog/ as slug.<h1>{params.slug}</h1>, so params.slug is hello-world.app/routes/profile.jsx but visiting /profile shows a 404 error. What is the most likely cause?/blog shows a list, and /blog/[postId] shows a post detail. How should you organize files in app/routes?index.jsx file for the base path.index.jsx inside blog folder for /blog, and [postId].jsx for dynamic post detail.blog with index.jsx and [postId].jsx inside it. -> Option CPass the test and this pattern moves unknown