React-Router-Dom Routes

We use the React-Routerarrow-up-right library to enable client-side routing, allowing the browser to manage page navigation without requesting new HTML documents from the server.

Unlike server-side routing, which involves full page reloads, React Router updates components and URLs on the client-side based on route configurations.

//Not included in React we install its components
npm i react-router-dom

//We change the name of Browser
import {
  BrowserRouter as Router,
  Link,
  Route,
  useParams,
  Routes,
  Outlet,
  useRouteError
} from "react-router-dom";

We configure the <Route/> URL path and element component in Router>Routes, any component containing React-Router components has to be inside of <Router/>.

The component outside the <Router> will remain during all route URL updates.

Fixed and Variable routes

Using <Outlet> on nested and /* paths in React Router

Let's check the properties of nested <Routes> and the properties of the /* catch-All parameter.

On reload Route components replace any previous element inside the <Router>. A parent Route component can render its child Route using <Outlet>, but any new <Route> that's not included in the nesting won't render any previous component.

Variable <Route/> path with <Outlet/> and absolute paths

The useParams() hook and nested <Routes on object arrays.

We access the current Route URL path with the useParams() hook, which returns a key-value object with the route path name and its url parameter respectively.

Instead of using the <Outlet> component we can nest routes by adding <Routes> on a element component.

chevron-right[Component] array render and content filter on useParams() path parameterhashtag

We render each object's name and set their id as :variable route path.

We can add an extra component as a fixed background and components for each avaiable route.

Each singular component shares the same filter structure, we render each array id as a new <Route> and include the next component as child component.

The useParams() values from the :variable <Route> filter and render the content.

Router variable Routes with variable components

The useLocation() hook and properties

The <Router> can contain any DOM tag (while <Routes> can only contain <Route/>).

Instead of the deprecated useHistory(), we import the useLocation() hook to access the current location object. It returns the URL pathname, a unique route key, an indiritation state, and both the search(?=) and the hash(#) in the URL.

The useLocation() hook updates only if a router-dom component is triggered. The browser navigation buttons update the URL using the history API and the <Router/> component tree, but it's not a router event.

The search property returns the search queryString from the URL.

The URLSearchParams is a built-in javascript interface for the query strings in the URL, It allows us to access and edit the returned key/value pairs.

Unlike useParams(), the useLocation() pathname includes both the static and dynamic routes.

The useNavigation() sibling <Route> and localStorage

The <Link> handles imperative redirecting, while the useNavigate() is for programatical redirecting.

The useNavigate() hook returns a function that redirects the user in response to a variety of events, not just clicking a link, its 2 arguments are the route path and the options object.

The replace property resets the URL to the current path and disables the previous route, while state is passed and accessed with useLocation().

A useNavigate() relative path will use history API to update the URL without updating the useLocation(), similarly to the browser navigation buttons.

We can also use the relative path as a shortcut between the sibling <Route>.

The localStorage is a browser-provided web storage object, accessible by the client and the server, persistent across browser sessions and <Router> routes.

1

Last updated