Cookie Consent by Free Privacy Policy Generator Update cookies preferences 📌 Diving into ReactJS: Understanding the Magic Behind Client and Server Routing.


📚 Diving into ReactJS: Understanding the Magic Behind Client and Server Routing.


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

What is Routing?

Routing in React.js refers to the process of managing navigation within a single-page application (SPA). In traditional multi-page web applications, each page corresponds to a different URL, and navigation between pages triggers a request to the server for a new HTML page. However, in SPAs built with React.js, all content is rendered on a single HTML page. Routing allows you to define different “routes” within your application based on the URL, so that different components are rendered based on the URL without a full page reload. This gives users a seamless browsing experience similar to traditional multi-page applications.

I’ll utilize the browser devtools to explore deeper into this article. Feel free to use any browser you prefer, but I’ll demonstrate using the Chrome browser. Simply right-click with your mouse or trackpad, select ‘Inspect,’ and then navigate to the Network tab.

What is Client-Side Routing?

Client-side routing refers to the management of page navigation or switching within the browser itself. For instance, when a user clicks a link that modifies the URL, the browser updates it without contacting the server, thus avoiding a full page refresh. This approach enables swift page transitions, with React Router being a widely used library for implementing client-side routing. To gain a better understanding of the client side, let’s begin by installing the React Router DOM package:

npm install react-router-dom

Once installed, import BrowserRouter from react-router-dom into your main.jsx file and enclose it around the parent component:


import { BrowserRouter } from “react-router-dom”;

<BrowserRouter>
   <App />
<BrowserRouter>

In the App component file, import Routes and Route from react-router-dom. To structure our routing page, let’s create a folder named components. Inside this folder, create two files: Home.jsx and RoutingExample.jsx, then import them into your App.jsx file. Wrap the files routing page inside the Routes property:


import react from “react”;

import { Routes, Route} from “react-router-dom”;
import Home from “./components/Home;
import RoutingExample from “./components/RoutingExample”;

const App = () => {
  return (
    <>
     <Routes>
       <Route path=“/“ element={<Home />}
       <Route path=“/routing-example“ element={<RoutingExample />/>
     </Routes>
    </>
)
}

Within the Home component, create a link that navigates to the RoutingExample page. Utilize the Link tag provided by react-router-dom:


import React from “react”;
import { Link } from “react-router-dom;

const Home = () => {

       return(
       <>
           <Link to=“/routing-example”>Client-side Routing</Link>
       </>
)
}

Remember the network tab I mentioned earlier? Let’s revisit it. On the count of one, two, three, click the link. Did you notice anything? Nothing happens inside the network tab that is no contact was made with the sever. The client-side routing ensures faster navigation between pages which made react router dom a widely used routing library.

Server-side routing on the other hand is managed by the server. When a user clicks a link, the browser sends a request to the server, which in turn responds with the relevant HTML page, leading to a complete page refresh. An example of this is the HTML anchor tag. This routing approach can lead to slower navigation due to the round-trips to the server involved. For server-side routing, you’ll need to utilise the HTML anchor tag. Begin by creating an index.html and routing.html file in your visual studio code or any code editor of your choice. In the index.html file, create a link using the traditional anchor tag with “Server-side Routing” as the inner text:

<a href=“routing.html”>Server-side Routing</a>

Return to your browser and revisit the network tab. Click on the link, and you’ll notice something remarkable: a full page refresh occurs before navigating to the routing.html page. This action triggers a request to the server, which then responds by providing the requested page.

In summary, client-side routing optimizes user experience with faster transitions and interactivity, while server-side routing focuses on SEO, initial load times, accessibility, and security. Combining both approaches can create a comprehensive solution that balances performance, SEO, and user experience.

...



📌 Diving into ReactJS: Understanding the Magic Behind Client and Server Routing.


📈 91.83 Punkte

📌 Diving into Type System behind Angular Signal Inputs


📈 33.67 Punkte

📌 Commonly asked ReactJS interview questions. Here are ReactJS interview questions and answers


📈 33.52 Punkte

📌 Diving into Server Actions in Next.js 14


📈 27.89 Punkte

📌 Apple stellt Magic Keyboard, Magic Trackpad und Magic Mouse in Spacegrau ein


📈 26.94 Punkte

📌 Space Gray: Magic Mouse, Magic Keyboard und Magic Trackpad in Sonderfarbe werden eingestellt


📈 26.94 Punkte

📌 Magic Mouse, Magic Keyboard, Magic Trackpad didn't get USB-C -- but it's still coming


📈 26.94 Punkte

📌 Apple: Magic Keyboard, Magic Mouse und Magic Trackpad immer noch mit Lightning unterwegs


📈 26.94 Punkte

📌 Open Routing: Facebook gibt interne Plattform für Backbone-Routing frei


📈 26.42 Punkte

📌 Kotlin Routing - routing everything


📈 26.42 Punkte

📌 Advanced Routing: Homematic IP bekommt intelligentes Routing


📈 26.42 Punkte

📌 Understanding ReactJS re-renders and memoization


📈 26.38 Punkte

📌 Understanding Reconciliation in ReactJs - Keeping the Virtual DOM and the Real DOM in Sync.


📈 26.38 Punkte

📌 Diving into an Old Exploit Chain and Discovering 3 new SIP-Bypass Vulnerabilities


📈 26.36 Punkte

📌 Python SDK: Diving into Workers and Workflows


📈 26.36 Punkte

📌 Diving Deeper into the Mostbet Experience: A Detailed Review of Gaming, Transactions, and User Interface in Pakistan


📈 26.36 Punkte

📌 Diving into TypeScript and Tailwind


📈 26.36 Punkte

📌 DIVING INTO THE NODE.JS EVENT LOOP AND CONCURRENCY MODEL


📈 26.36 Punkte

📌 Diving Into Capacitor 6: What’s New, What’s Improved, and How to Upgrade


📈 26.36 Punkte

📌 How to Install Winscp FTP Client/SFTP Client/SCP Client (WinSCP Client) In Ubuntu


📈 26.06 Punkte

📌 The Fundamentals Of ReactJS: A Rich Understanding Of Its Basics


📈 24.86 Punkte

📌 VB2016 paper: Diving into Pinkslipbot's latest campaign


📈 24.83 Punkte

📌 Diving Into Zberp’s Unconventional Process Injection Technique


📈 24.83 Punkte

📌 ICS Sandbox: Diving into the ICS Threat Landscape at RSA Conference


📈 24.83 Punkte

📌 PSX - Diving into custom CDROM I/O functions in Xenogears


📈 24.83 Punkte

📌 Diving Deep Into a Pwn2Own Winning WebKit Bug


📈 24.83 Punkte

📌 Diving Deep Into a Pwn2Own Winning WebKit Bug


📈 24.83 Punkte

📌 George Soros, Rockefeller Take Their Marks Before Diving Into the Cryptocurrency Pool


📈 24.83 Punkte

📌 Reversing WannaCry Part 2 - Diving deeper into the malware with #Ghidra


📈 24.83 Punkte

📌 Diving into malicious autoit code, author: xavier mertens


📈 24.83 Punkte

📌 Diving into /proc/pid/mem


📈 24.83 Punkte

📌 Diving deeper into Azure cost optimization (Part 1) | Well-Architected Framework


📈 24.83 Punkte

📌 Diving deeper into Azure cost optimization (Part 2) | Well-Architected Framework


📈 24.83 Punkte











matomo