about
page:about
page.Advantage: Faster performance. No Full Page Reload: the page doesn’t undergo a full reload. Only the necessary components and data are swapped out, resulting in a smoother and faster user experience.
Through client-side routing, the web app downloads, processes, and displays fresh data seamlessly.
“Client side routing is a type of routing where as the user navigates around the application or website no full page reloads take place, even when the page’s URL changes. Instead, JavaScript is used to update the URL and fetch and display new content” - Will Taylor
“React Router is a fully-featured client and server-side routing library for React, a JavaScript library for building user interfaces. React Router runs anywhere React runs; on the web, on the server with node.js, and on React Native.” React Router Docs
⬇️
Before we do anything, we need to make a plan about what we want.
What URLs do we want our application to have to simulate the feeling of different “pages”?
Component | Url |
---|---|
Home | / (root route) |
ProjectForm | /projects/new |
ProjectEditForm | /projects/:id/edit |
ProjectDetail | /projects/:id |
ProjectList | /projects |
The Primary React Router Components
BrowserRouter
Routes
Route
Link
import React from "react";
import "./index.css";
import App from "./App";
import * as ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
1️⃣ BrowserRouter
💡 BrowserRouter
is a wrapper for App to allow conditional rendering based on the URL
❓ Where does it belong?
Since App
component is imported and mounted inside the Index.js
file, this is a great place to wrap the component within BrowserRouter
return (
<Routes>
<Route path="/"
element={<Home />} />
<Route path="/projects"
element={<ProjectList
projects={projects}
/>} />
<Route path="/projects/new"
element={<ProjectForm
onAddProject={onAddProject} />} />
</Routes>
);
Routes
is a wrapper for Routes. Looks through all its child Route components and returns the one that matches the current URLreturn (
<Routes>
<Route path="/"
element={<Home />} />
<Route path="/projects"
element={<ProjectList
projects={projects}
/>} />
<Route path="/projects/new"
element={<ProjectForm
onAddProject={onAddProject} />} />
</Routes>
);
Route
wraps content that should be visible when the designated route is active.route
belong?Every component nested inside of the Routes
component will be individually wrapped inside of a Route
component.
💥 Route will be provided a path
prop. This is where the developer can define the URL associated with the component. That means that if the pattern of the URL matches the path defined, the component will render
<a className="button" href="/projects">
View All Projects
</a>
<Link to ="/projects"
className="button" >
View All Projects
</Link>
💡 Link
creates an anchor tag in your application that will navigate using React Router instead of the browser default
We want to use Link
for navigation in our application. It will ensure that a page reload does not occur, unlike the use of an anchor tag <a></a>
💥 Link will be provided a to
prop which indicates where the link should navigate to (generates the href)
import { useParams } from "react-router-dom";
const ProjectDetail = () => {
const { id } = useParams()
//use parameter
//For example, if the URL is: localhost:3000/projects/18
// id will be 18
return (
<h1>
{id}
</h1>
)};
The useParam
hook retrieves the route parameter from the URL. This allows us to access /:id
, which represents dynamic values embedded within the URL, and is often used to identify specific resources.