Introduction to React Router and its Evolution
React Router is a powerful and declarative library for managing navigation in React applications. It allows developers to create single-page applications (SPAs) with a smooth and user-friendly experience. Over time, the library has evolved, with significant changes introduced in version 6. This tutorial focuses on one crucial change: the replacement of the <Switch>
component with <Routes>
. We’ll explain the reasons behind this change and how to adapt your code to the latest version.
The Role of <Switch>
in React Router v5
Prior to version 6, the <Switch>
component was the standard way to define a set of routes, rendering the first route that matched the current location. It essentially acted as an exclusive OR gate for routes. Only one route within the <Switch>
could be rendered at a time. This was particularly useful when you wanted to define a catch-all route (like a 404 page) that would only render if no other routes matched.
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
function App() {
return (
<Router>
<Switch>
<Route path="/home">
{/* Component to render for /home */}
</Route>
<Route path="/about">
{/* Component to render for /about */}
</Route>
<Route path="*">
{/* 404 Component */}
</Route>
</Switch>
</Router>
);
}
Why the Change to <Routes>
in React Router v6?
The React Router team redesigned the routing mechanism in version 6 to improve performance, simplify the API, and take advantage of newer React features. <Routes>
achieves a similar outcome to <Switch>
, but with a more consistent and predictable behavior. Crucially, in v6, <Routes>
always renders all matching routes. This behavior is more aligned with how React components generally work. The order of routes still matters, as the first matched route will receive focus if that is needed.
Implementing <Routes>
in React Router v6
Here’s how you would rewrite the example above using <Routes>
:
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
function App() {
return (
<Router>
<Routes>
<Route path="/home">
{/* Component to render for /home */}
</Route>
<Route path="/about">
{/* Component to render for /about */}
</Route>
<Route path="*">
{/* 404 Component */}
</Route>
</Routes>
</Router>
);
}
Notice the key changes:
Switch
is replaced withRoutes
.- The
component
prop on<Route>
is replaced with theelement
prop. Theelement
prop accepts a React element (e.g.,<Home />
) instead of a component function.
Key Differences and Best Practices
- Exclusive vs. Inclusive Matching: Remember that
<Switch>
rendered only the first matching route, while<Routes>
renders all matching routes. This is a fundamental shift in how routing is handled. component
vs.element
: Use theelement
prop in<Route>
to specify the React element to render. This is the recommended approach in React Router v6.- Order Matters: While
<Routes>
renders all matching routes, the order still matters. React Router will render routes from top to bottom and will render the first matching route with focus if the route requires focus. exact
Prop: Theexact
prop is generally no longer needed in v6 due to the improved matching algorithm. React Router is more precise in determining route matches.
Example with Functional Components and JSX
Here’s a more complete example using functional components and JSX:
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
function Home() {
return <h1>Home Page</h1>;
}
function About() {
return <h1>About Page</h1>;
}
function NotFound() {
return <h1>404 - Not Found</h1>;
}
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Routes>
</Router>
);
}
export default App;
Conclusion
The transition from <Switch>
to <Routes>
in React Router v6 represents a significant improvement in the library’s design. By understanding the key differences and adopting the new approach, you can build more efficient and maintainable React applications. Remember to consult the official React Router documentation for the most up-to-date information and best practices.