31
Comparing React Router 5, 6, and React Location
Recently React Router released version 6 which created a lot of confusion as several aspects of its API are quite different. Also, Tanstack released React-Location, an entrant to React Routing space from the creators of beloved libraries like React-Query, React-Table, React-Charts, etc. So let's see how we'd install and do common routing tasks with all three.
npm install react-router-dom@5.3.0
npm install react-router-dom
npm install react-location
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import {BrowserRouter as Router} from "react-router-dom"
import reportWebVitals from "./reportWebVitals";
ReactDOM.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>,
document.getElementById("root")
);
import {ReactLocation} from "react-location"
import AboutPage from "./pages/About"
import HomePage from "./pages/Home"
// create a location object
export const location = new ReactLocation()
// create a routes object
export const routes = [
{ path: "/", element: <HomePage /> },
{ path: "/about", element: <AboutPage/>}
]
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Router } from "react-location";
import { location, routes } from "./location";
ReactDOM.render(
<Router location={location} routes={routes}>
<React.StrictMode>
<App />
</React.StrictMode>
</Router>,
document.getElementById("root")
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
<Switch>
<Route path="/about" component={About}/>
</Swich>
<Switch>
<Route path="/about">
<About/>
</Route>
</Swich>
<Switch>
<Route path="/about" render={(routerProps) => <About {...routerProps}/>}/>
</Swich>
<Routes>
<Route path="/about" element={<About/>}>
<Routes>
// create a routes object
export const routes = [
{ path: "/", element: <HomePage /> },
{ path: "/about", element: <AboutPage/>}
]
the defined routes will appear wherever you place the Outlet component
import {Outlet} from "react-location"
function App() {
return (
<div className="App">
<Outlet/>
</div>
);
}
export default App;
In all three scenarios, params are part of the url marked with colons
/about/:person
which gets saved in a variable of the same name. How to access the param can differ.In order to get the param you need access to the match prop which will only exist in the component if the route using the "component" or "render" prop form. So using the above url path as an example. We can then retreive the param from props like so.
const person = props.match.params.person
We can just use the useParams hook to get the params object and grab what we need.
import {useParams} from "react-router-dom"
const params = useParams()
const person = params.person
This is similar to react router 6, you can get the match object using the useMatch hook and pull params from there.
import {useMatch} from "react-location"
const match = useMatch()
const params = match.params
const person = params.person
All three scenarios can use a
<Link>
component to link from one route to another, but what happens when you want to redirect from inside a function?This would need the router props meaning you need to use the "component" or "render" prop route forms, and the function to redirect the user to a different route would be
props.history.push("/")
You just use the useNavigate hook to pull in the navigate function then redirect.
import {useNavigate} from "..."
const navigate = useNavigate()
navigate("/")
Hopefully this helps show you the many options you have for routing with React, but of course you can always use Gatsby or NextJS and take advantage of file-based routing with React!
31