⊗jsrtPmRtCSR 28 of 47 menu

Client-side Routing in React Router

In this lesson, we will look at an important component of single-page applications (SPA) - client-side routing. Such routing allows us to update the URL in the browser without additional document requests from the server.

Run the application we created in the previous lesson. Open the developer panel and in it the tab 'Network' (Network for Chrome). After each click on our links, we see a request for document. That is, each time we ask the server to load document for us.

Now let's take advantage of React Router to get rid of such requests. To do this, open up root.jsx of our app and replace the a markup tag with the Link component. First, let's add its import:

import { Outlet, Link } from 'react-router-dom';

Now let's replace the a tags with the href attribute. Instead of the code snippet:

<a href={`/products/1`}>Product1</a> <a href={`/products/2`}>Product2</a>

We will now have the following code:

<Link to={`/products/1`}>Product1</Link> <Link to={`/products/2`}>Product2</Link>

We open the 'Network' tab on the developer panel again, set the root path in the address bar and refresh the page. Now we click on the links and see that document was requested only once during the initial loading and there is no request to the server during subsequent clicks.

Take the application you created in the previous lessons. Using the lesson materials, implement client-side routing in it, reworking your links as described in the lesson.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline