Hooks of React Router

Posted on January 18, 2020

React Router 5 embraces the power of hooks and has introduces four different hooks to help with your routing.

But before we look at hooks themselves, we have to look at a new pattern that the Route component exposes and how this changes things.

Before:

// When you wanted to render the route and get router props for component
<Route path="/" component={Home} />
// Or when you wanted to pass extra props
<Route path="/" render={({ match }) => <Profile match={match} mine={true} />}>

When using the component syntax, route props (match, location and history) are implicitly being passed on to the component. But it has to be changed to render once you have extra props to pass to the component. Note that adding a function to the component syntax would lead to the component re-mounting on every render.

After:

<Route path="/">
<Home />
</Route>

Note that there is no implicit passing of any props to the Home component. But without changing anything with the Route itself, you can add any extra props to the Home component. You can no longer make the mistake of re-mounting the component on every render and that’s the best kind of APIs.

But now that implicit passing of route props is not there, how do we access match, history or location? Do we have to wrap all components with withRouter? That is where the hooks steps in.

Note that hooks were introduced in 16.8 version of React, so you need to be above that version to use them.

useHistory

  • Provides access to the history prop in React Router
  • Refers to history package dependency that the router uses
  • Primary use case would be for programmatic routing with functions like push, replace etc
import { useHistory } from 'react-router-dom';
function Home() {
const history = useHistory();
return <button onClick={() => history.push('/profile')}>Profile</button>;
}

useLocation

  • Provides access to the location prop in React Router
  • It is similar to the window.location in the browser itself, but this is accessible everywhere as it represents the Router state and location.
  • Primary use case for this would be to access the query params or the complete route string.
1import { useLocation } from 'react-router-dom';
2
3function Profile() {
4 const location = useLocation();
5 useEffect(() => {
6 const currentPath = location.pathname;
7 const searchParams = new URLSearchParams(location.search);
8 }, [location]);
9 return <p>Profile</p>;
10}

Since location property is immutable, the useEffect will call the function everytime the route changes, making it perfect to operate on search parameters or current path.

useParams

  • Provides access to search parameters in the URL.
  • This was possible earlier only using match.params
1import { useParams, Route } from 'react-router-dom';
2
3function Profile() {
4 const { name } = useParams();
5 return <p>{name}'s Profile</p>;
6}
7
8function Dashboard() {
9 return (
10 <>
11 <nav>
12 <Link to={`/profile/ann`}>Ann's Profile</Link>
13 </nav>
14 <main>
15 <Route path="/profile/:name">
16 <Profile />
17 </Route>
18 </main>
19 </>
20 );
21}

useRouteMatch

  • Provides access to the match object.
  • If provided with no arguments, it returns the closest match in the component or it’s parents.
  • Primary use case would be to construct nested paths.
1import { useRouteMatch, Route } from 'react-router-dom';
2
3function Auth() {
4 const match = useRouteMatch();
5 return (
6 <>
7 <Route path={`${match.url}/login`}>
8 <Login />
9 </Route>
10 <Route path={`${match.url}/register`}>
11 <Register />
12 </Route>
13 </>
14 );
15}

You can also use useRouteMatch to access a match without rendering a Route, this is done by passing it the location argument.

For example, consider that you need your own profile to be rendered at /profile and somebody else’s profile if the URL contains the name of the person /profile/dan or /profile/ann. Without using the hook, you would either write a Switch and list down both routes and customise them with props, now using the hook:

1import {
2 Route,
3 BrowserRouter as Router,
4 Link,
5 useRouteMatch,
6} from 'react-router-dom';
7
8function Profile() {
9 const match = useRouteMatch('/profile/:name');
10
11 return match ? <p>{match.params.name}'s Profile</p> : <p>My own profile</p>;
12}
13
14export default function App() {
15 return (
16 <Router>
17 <nav>
18 <Link to="/profile">My Profile</Link>
19 <br />
20 <Link to={`/profile/ann`}>Ann's Profile</Link>
21 </nav>
22 <Route path="/profile">
23 <Profile />
24 </Route>
25 </Router>
26 );
27}

You can also use all the props on Route like exact or sensitive as an object with useRouteMatch.


Using the hooks and the technique we mentioned in the beginning with Route would make it much easier to upgrade to new versions of React Router.

Buy me a coffeeBuy me a coffee