· react
Upgrade to React Router v6
React Router Version 6 is great for TypeScript programmers because it comes with type definitions. It also introduces the `useRoutes` hook, which simplifies routing setups in functional React components. Additionally, the new `Outlet` API allows for rendering child components. The article provides examples of how routing was done in React Router v5 and how it has changed in v6.
React Router Version 6 is great for TypeScript programmers because it ships with type definitions. Another great feature is the
useRoutes
hook, which simplifies routing setups in your functional React components. You can also render child components by using the new
Outlet
API.
Contents
match
Before React Router v6
Prior to React Router v6, you had to install external type definitions along with the React Router packages:
@types/react-router-dom
)
This is an example of how routing with React Router v5 was done:
Router component
Route configuration
Route setup separated by layouts. Routes are evaluated in order. The
Switch
component guarantees that there are only exclusive matches. No other
Route
definition will be rendered if there is a match. If there is no match, the wildcard route of
*
will be used to render a page not found view:
Links and Layouts
The main layout shows how to link to different views by using the
Link
component:
The account layout defines child routes of the application with their respective components. Pay attention to the order of the routes:
/account/:id
comes last because the
:id
parameter will match anything after
/account
and would also match
/account/add
and others.
Props from routes
If you want to use matching parameters from your routes, you will have to define
RouteComponentProps
in your React component:
React Router v6
Installation
You don't need to install additional typings with React Router v6. You will be good by adding just these two packages to your web application:
Router
You will have to wrap your main app component in a Router component:
If you don't follow this rule, you are likely to run into the following error:
Uncaught Error: useRoutes() may be used only in the context of a
Nested Routing
When your app is wrapped in a
<Router>
component, you can define its routes. React Router v6 provides a
useRoutes
hook to do that:
By looking at the code above, you may have noticed that React Router supports nested routing where you can define routes for different parts of your application with different layouts. This is possible because of the
<Outlet />
component, which is a placeholder for the elements that should be rendered on the child routes / paths.
Here is the code of the
AccountLayout
component to showcase the new Outlet API:
Navigation
Navigation between different views is done with the
Link
component, which uses
navigate
under the hood and is the preferred way to make URL navigations. The use of
history
and
useHistory
is deprecated and should be replaced with the
useNavigate
hook. The React Router team provides a
Migration Guide
in this regard.
Props and
match
There is no more need to extend your component's props with the properties of
match
. You can retrieve parameters from your routing with the
useParams
hook: