Switch Component (React Router v5)
Role: Renders the first matching <Route> or <Redirect> child exclusively
Behavior:
Stops searching for matches after the first route match is found
Order of routes matters (more specific paths should come first)
Doesn't support relative routing
Requires exact prop for strict path matching
import { Switch, Route } from 'react-router-dom';
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route component={NotFound} /> {/* Catch-all route */}
</Switch>
Routes Component (React Router v6)
Role: Replacement for Switch with smarter matching and new features
Key Improvements:
Uses best match algorithm instead of first match
Automatically handles relative routing and nested routes
No need for exact prop (paths match exactly by default)
Requires element prop instead of component/render
Better TypeScript support
import { Routes, Route } from 'react-router-dom';
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NotFound />} /> {/* Catch-all route */}
</Routes>