React-Router-Dom Routes
We use the React-Router 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.

The <Link> component renders an anchor tag, on click, it updates the URL and route based on the to prop, handling the process declaratively (i.e., specifying what to do, not how to do it) and avoiding a full page reload.
The route path can be either fixed and explicit or dynamic, where certain parts of the URL are replaced with :variables that are later defined by the values provided in the <Link> to prop.
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.
When multiple routes match a given URL, the more specific route will take priority.
A catch-all parameter (/*) matches any path that shares the same previous URL , capturing additional parameters and acting as a fallback route when no other suitable path routes exist.
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.
We can use URL path parameters to filter imported arrays using imported functions.
[Component] array render and content filter on useParams() path parameter
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.
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.
The hash value is separated from the URL by a (#) fragment identifier.
It updates the URL without sending an HTTP request to the server nor reloading the browser, used for single-page navigation.
We can use it to log events based on the anchor ID the user has been redirected to.
Hash sections url are also used for SEO google search optimitation.
The <Link> won't trigger an ID hash scroll because it doesn't update the URL with a hash, but it replaces the current URL with another one that already includes a hash.
The state prop sends data between <Link> routes, It won't persist on reload or route change. It's easily accessible so it's better to use localStorage and sessionStorage with encryption for user data.
When a component is mounted by the <Router>, a unique read-only key identifier is created based on the current URL.
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