React Router
React Router
💡 Why Should You Learn This?
Concept
React Router v6 is the modern standard for SPA navigation, with nested routing and layout management via Outlet, and data-fetching optimization via loader being its core strengths. It is an essential routing architecture skill for large-scale projects.
Why It Matters
When building admin panels or dashboards in real-world projects, nested layouts with a sidebar-header-content structure are indispensable. The loader pattern lets you pre-load data before entering a page, significantly improving UX. This is a must-know technology, especially for projects that choose React SPA over Next.js.
Core Concepts
React Router v6 is similar to an apartment building structure. Nested routing is a hierarchical structure like "building-unit-room," Outlet is the "empty slot" on each floor where child components are rendered, and loader is like preparing everything you need before entering a room.
Key Points
- ▸Nested routing aligns URL hierarchy with component structure
- ▸Parent-child layout management via the Outlet component
- ▸Pre-load data before component rendering using loader functions
💡 ⚠️ Common Mistakes
- ▸Forgetting Outlet so nested routes never render — you must add
to the parent component - ▸Not handling errors in loader, causing the app to crash — errors must be thrown as Response objects inside a try-catch
- ▸Confusing index: true with path: '' — index is the default for the parent path, while path: '' matches all child paths
💡 🎯 Interview Prep
Q: Explain the difference between nested routing in React Router v6 and v5
Q: Describe the role and usage of the Outlet component
Q: What are the reasons and benefits of using the loader pattern?
Hint: For nested routing, lead with aligning URL structure with component hierarchy and improved layout reusability. For Outlet, mention specifying where child components render. For loader, highlight pre-loading data before component rendering to improve UX — build your answers around these key terms.
⚛️ React Pattern — React Router
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
<Link to="/about">About</Link>