React Router is widely appreciated in React apps for handling routing and navigation. It provides a way to configure routes. Show components based on the current URL. The newest release, React Router v6 introduces changes and improvements compared to the version of React Router v5. This article will explore the differences between React Router v6 and v5.
Comparison of React Router v6 vs React Router v5:
React Router version 5 is a used library in many React applications offering components and tools for managing routing and navigation. Developers can set up routes display components based on those routes manage navigation events and share route parameters with components using React Router v5. On the other hand, React Router version 6 is the most recent update to the library bringing various enhancements and changes. Its goal is to streamline routing processes and present a user-friendly interface. React Router v6 introduces an approach to routing known as "Component Based Routing " removing the distinction, between "route components" and "route rendering components" seen in v5.
- In React Router version 5 navigation is managed through the <Router> <Switch> and <Route> elements. The precise attribute, in the <Route> element guarantees that only the exact path is matched.
- In React Router version 6 the <Router> component stays consistent however the <Switch> component gets substituted by the <Routes> component. The element attribute is employed in the <Route> component to display the component.
React Router v5:
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';function App() {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
</Router>
);
}React Router v6:
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Router>
);
}Key Features of React Router:
React Router v5:
- Nested routing
- Router parameters
- Query parameters
- Router guarding (using route component's render prop )
React Router v6:
- Simpler and more intuitive API
- Improved code splitting and lazy loading
- Relative routes
- Parallel routing
- Automatic scrolling to top on navigation
Upgrades from React Router V5 to V6
Switch Replaced with Routes:
React Router v6 introduces an update by swapping out the <Switch> component with the <Routes> component. Unlike in React Router v5 where the <Switch> component exclusively displayed the matching route in React Router v6, the <Routes> component is utilized to outline all routes in our application. This change provides flexibility and control, over how routes are structured and nested.
In version 5 we did it like this:
JavaScript
import { BrowserRouter, Switch } from "react-router-dom";
function App() {
return (
<BrowserRouter>
<div className="App">
<Switch>
{" "}
{/* Single Routes come in here */}
</Switch>
</div>
</BrowserRouter>
);
}
export default App;