How the routing works
react-router-dom
library for this purpose. This document provides a general overview of how routing is structured and implemented.
BrowserRouter
component which enables the use of HTML5 history API. Inside it, we define our routes using the Routes
and Route
components.
Route
is defined with a path
prop that denotes the URL path and an element
>
prop that renders the corresponding component when the path is matched.
ChildComponent
is nested within ParentComponent
, and it will inherit any layouts or context provided by ParentComponent
.
ProtectedRoute
is a higher-order component that wraps around the protected component. It checks for the specified conditions and either renders the component or redirects the user.
NewComponent
.Routes
component, insert a new Route
.react-router-dom
provides the useNavigate
hook.
DashboardPage
with a SidebarNavigation
), and you want to add a new route (NewComponent
) within this layout, you can follow these steps:
NewComponent
.Route
nested within the layout component’s Route
.NewComponent
will be accessible at /dashboard/new-path
and will inherit the DashboardPage
layout, which includes the SidebarNavigation
.
The DashboardPage
component uses the <Outlet />
component to render its child routes:
<Outlet />
component is where the child routes get rendered. In this case, NewComponent
will be rendered inside the MainLayout
of the DashboardPage
.