React Router Hooks
Why#
With React Router, we can create navigation to different webpages by adding routes, a Routes, and a BrowserRouter. React Router ships with a few hooks that let you access the state of the router and perform navigation from inside your components.
What#
There are four primary router hooks we can use:
useNavigateuseLocationuseParams
We should recognize these four as React hooks by their naming conventions: use. Let’s dive into each one.
useNavigate#
The useNavigate hook allows you to navigate routes programmatically. You can use the returned function by passing in a route path (string), and optional options object.
Example use cases:
navigate(“/game”)- navigates to the game routenavigate(“/game”, { replace: true, state: 10 })- navigates to the game route, replaces the current route, and passes the value of 10 with the navigationnavigate(-1)- navigates to the previous route (back)
useLocation#
The useLocation hook allows you to access the location object that represents the current URL.
Let’s take a look at the location object.
You can use the useLocation hook whenever you need to track pathname changes in your web application.
useParams#
The useParams hook allows you to access the key/value pairs of URL parameters. You can use this to access match.params of the current route inside your component.
First, let’s add a parameter to my route:
Let’s take a look at the params object if the url is "http://localhost:3000/characters/kvothe":
Now I can use the useParam hook to access params.name:
How#
Let’s go through it step by step.
We need to make sure the react-router-dommodule is installed:
Next we need to go to App.jsx and import our Browser router, and then wrap everything in our App component with the BrowserRouter component:
Next we need to register our Routes inside of the Routes component, which means to tell React which components to render based on the given routes, so in App.jsx add the Routes and Route components to the import statement:
And we’ll add our router components in the return statement:
Now, let’s create our Codewars component.
Import useParams, useState, and useEffect.
We’ll create a functional component called Codewars, where we’ll use useState for user and isLoaded values. We’ll also create a variable called controller to assign a new AbortController instance. The component will return basic information about the codewars user that we store in state.
Now let’s use the useEffect to call the Codewars API based on the ‘username’ url parameter that we can pull from the useParams hook.
Great! This Codewars component now will call a fetch request depending on the url parameter that is provided by the react router. If you think about it, we now have an indefinite amount of Codewars routes!
Takeaways#
- React Router comes with its own hooks for accessing object values related to site navigation and page views
useNavigateis a hook for navigating programmicallyuseParamsis a hook for accessing the url parametersuseLocationis a hook for accessing url location values such as pathname, hash, query, and state