<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Alex Anie - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Alex Anie - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 17 May 2026 04:37:29 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/alex-anie/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Set up Swiper Element in a React Application ]]>
                </title>
                <description>
                    <![CDATA[ Carousels or image sliders are an integral part of many web applications. They help you group multiple elements in a single view.  Using carousels, you can implement image slides, video slides, text slides, interactive image galleries, product catalo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-set-up-swiper-element-in-a-react-application/</link>
                <guid isPermaLink="false">66c5f68e1bceda0ea5c3977e</guid>
                
                    <category>
                        <![CDATA[ animations ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Alex Anie ]]>
                </dc:creator>
                <pubDate>Mon, 19 Feb 2024 16:25:55 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/How-to-Set-up-Swiper-Element-in-React-Application-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Carousels or image sliders are an integral part of many web applications. They help you group multiple elements in a single view. </p>
<p>Using carousels, you can implement image slides, video slides, text slides, interactive image galleries, product catalogs, team profiles, and more.</p>
<p><a target="_blank" href="https://swiperjs.com/">Swiper.js</a> comes with powerful functionalities that make it easy to create functional, reusable carousel components with stunning effects and functionality. You can enable zooming in and out from images, horizontal and vertical image scrolling, parallax slide effects, and infinite loop slides, just to mention a few.           </p>
<p>In this tutorial, we’ll be focusing on the new version of Swiper, 11.0.6, and the new Swiper Element that is recommended for building slides in Swiper.js. </p>
<p>By the end of this guide, you should be able to implement the new Swiper Element from Swiper.js to create responsive, dynamic slides with pre-made effects in your React project.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-what-is-swiperjs">What is Swiper.js?</a></li>
<li><a class="post-section-overview" href="#heading-installations">Installations</a></li>
<li><a class="post-section-overview" href="#heading-how-to-set-up-swiper">How to Set Up Swiper</a><ul>
<li><a class="post-section-overview" href="#heading-node-modules">Node Modules</a></li>
<li><a class="post-section-overview" href="#heading-swiper-custom-elements-from-cdn">Swiper Custom Elements from CDN</a></li>
<li><a class="post-section-overview" href="#heading-swiper-custom-elements">Swiper Custom Elements</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-swiper-parameters-as-attributes">Swiper Parameters as Attributes</a></li>
<li><a class="post-section-overview" href="#heading-responsive-breakpoints-in-swiper-element">Responsive Breakpoints in Swiper Element</a></li>
<li><a class="post-section-overview" href="#heading-pagination-and-navigation">Pagination and Navigation</a></li>
<li><a class="post-section-overview" href="#heading-lazy-loading-images">Lazy Loading Images</a></li>
<li><a class="post-section-overview" href="#heading-effects-in-swiper-element">Effects in Swiper Element</a></li>
<li><a class="post-section-overview" href="#heading-vertical-sliders">Vertical Sliders</a></li>
<li><a class="post-section-overview" href="#heading-how-to-style-swiper-element">How to Style Swiper Element</a><ul>
<li><a class="post-section-overview" href="#heading-reset-swiper-custom-styles">Reset Swiper Custom Styles</a></li>
<li><a class="post-section-overview" href="#heading-create-a-new-class-name">Create a New Class Name</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-how-to-build-a-custom-slider-element">How to Build a Custom Slider Element</a></li>
<li><a class="post-section-overview" href="#heading-summary">Summary</a></li>
<li><a class="post-section-overview" href="#heading-references">References</a></li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before you get started, you'll need to have basic knowledge of <a target="_blank" href="https://react.dev/">React.js</a>.</p>
<p>If you need a refresher, you can check out this <a target="_blank" href="https://www.freecodecamp.org/news/react-beginner-handbook/">React handbook for beginners</a>.</p>
<h2 id="heading-what-is-swiperjs">What is Swiper.js?</h2>
<p>Swiper is a free, modern JavaScript library for building touch sliders (Carousels) with hardware transitions and amazing native behaviors. </p>
<p>Swiper is built for mobile websites, mobile web apps, and mobile native/hybrid apps. Swiper also comes with great support and functionalities for Desktop websites and web applications. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Untitled.png" alt="Migrating from Swiper React Component to Swiper Element Guide" width="600" height="400" loading="lazy"></p>
<p>As of the time of writing, Swiper <em>v11.0.6</em> recommends migrating to <a target="_blank" href="https://swiperjs.com/element">Swiper Element</a> instead of <a target="_blank" href="https://swiperjs.com/react">Swiper React Components</a>, which will likely be removed in future versions. In this tutorial, we’ll be focusing on Swiper Element and its use cases. </p>
<h2 id="heading-installations">Installations</h2>
<p>To get started, generate some React boilerplate code with <a target="_blank" href="https://vitejs.dev/guide/">Vite.js</a>. Next, navigate to the project directory in your Terminal and type the command below to install Swiper.js:</p>
<pre><code class="lang-bash">npm i swiper
</code></pre>
<p>Next, type the command below to launch your React.js boilerplate code:</p>
<pre><code class="lang-bash">npm run dev
</code></pre>
<p>Click on the port URL on the Terminal to open up your React boilerplate code on the browser. </p>
<h2 id="heading-how-to-set-up-swiper">How to Set Up Swiper</h2>
<p>To use Swiper in a React project, you can use the following methods.</p>
<h3 id="heading-node-modules">Node Modules</h3>
<p>If you install Swiper via <code>npm i swiper</code>, then <code>import</code> the custom element from <em>node_modules</em> and register it. </p>
<pre><code class="lang-jsx">├─ main.jsx

<span class="hljs-comment">// import function to register Swiper custom elements</span>
<span class="hljs-keyword">import</span> { register } <span class="hljs-keyword">from</span> <span class="hljs-string">'swiper/element/bundle'</span>;
<span class="hljs-comment">// register Swiper custom elements</span>
register();
</code></pre>
<p>This should be done once directly on the <code>main.jsx</code> file to install it globally.</p>
<h3 id="heading-swiper-custom-elements-from-cdn">Swiper Custom Elements from CDN</h3>
<p>To enable Swiper Element, include the CDN link in the script tag as indicated below:</p>
<pre><code class="lang-jsx">├─ index.html
&lt;head&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/swiper@11/swiper-element-bundle.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
&lt;/head&gt;
</code></pre>
<p>Do this once directly on the <code>index.html</code> file to enable it globally.</p>
<h2 id="heading-swiper-custom-elements">Swiper Custom Elements</h2>
<p>If you’ve successfully set up Swiper Element, and call the <code>register()</code> method. To create a slider, you'll need to use <code>&lt;swiper-container&gt;</code> and <code>&lt;swiper-slide&gt;</code> from Swiper.js.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide1<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide2<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide3<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide4<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide5<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>The code above shows how to structure slides using Swiper Custom Elements. </p>
<ul>
<li><code>&lt;swiper-container&gt;</code>: This is the parent element that serves as a container to the <code>&lt;swiper-slide&gt;</code> Custom Element and other HTML Elements that make up the slides. Any Element that is nested inside the <code>&lt;swiper-container&gt;</code> Custom Element is treated as a slide.</li>
<li><code>&lt;swiper-slide&gt;</code>: This is the direct child of the <code>&lt;swiper-container&gt;</code> Custom Element. The <code>&lt;swiper-slide&gt;</code> serves as the individual slide of the slider components.</li>
</ul>
<p>For clarity purposes, I have added some custom CSS styles to make it visually appealing and easy to understand. Copy the code below: 👇</p>
<pre><code class="lang-css">*,*<span class="hljs-selector-pseudo">::before</span>, *<span class="hljs-selector-pseudo">::after</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">box-sizing</span>: border-box;
    <span class="hljs-attribute">font-family</span>: calibri;
}

<span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">justify-content</span>: center;
}

<span class="hljs-selector-tag">swiper-container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">text-align</span>: center;
}

<span class="hljs-selector-tag">swiper-slide</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">4</span> <span class="hljs-number">47</span> <span class="hljs-number">46</span>);
    <span class="hljs-attribute">color</span>: <span class="hljs-number">#fff</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
}

<span class="hljs-selector-tag">swiper-slide</span><span class="hljs-selector-pseudo">:nth-child(2)</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">15</span> <span class="hljs-number">118</span> <span class="hljs-number">110</span>);
}

<span class="hljs-selector-tag">swiper-slide</span><span class="hljs-selector-pseudo">:nth-child(3)</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">45</span> <span class="hljs-number">212</span> <span class="hljs-number">191</span>);
}

<span class="hljs-selector-tag">swiper-slide</span><span class="hljs-selector-pseudo">:nth-child(4)</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">153</span> <span class="hljs-number">246</span> <span class="hljs-number">228</span>);
}

<span class="hljs-selector-tag">swiper-slide</span><span class="hljs-selector-pseudo">:nth-child(5)</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">34</span> <span class="hljs-number">197</span> <span class="hljs-number">94</span>);
}
</code></pre>
<p>Your code should look just like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Swiper_custome_Elements.gif" alt="Slide shows created slider with Swiper Element" width="600" height="400" loading="lazy"></p>
<h2 id="heading-swiper-parameters-as-attributes">Swiper Parameters As Attributes</h2>
<p><a target="_blank" href="https://swiperjs.com/element">Parameters</a> in Swiper are <em>key</em> and <em>value</em> or <em>key</em>, <em>subkey</em>, and <em>value</em> pairs as indicated below.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> swiper = <span class="hljs-keyword">new</span> Swiper(<span class="hljs-string">'.swiper'</span>, {
  <span class="hljs-attr">scrollbar</span>: {
    <span class="hljs-attr">el</span>: <span class="hljs-string">'.swiper-scrollbar'</span>,
    <span class="hljs-attr">draggable</span>: <span class="hljs-literal">true</span>,
  },
<span class="hljs-attr">mousewheel</span>: {
    <span class="hljs-attr">invert</span>: <span class="hljs-literal">true</span>,
  },
<span class="hljs-attr">slidesPerView</span>: <span class="hljs-number">3</span>,
<span class="hljs-attr">spaceBetween</span>: <span class="hljs-number">20</span>,
<span class="hljs-attr">scrollbar</span>: {
    <span class="hljs-attr">clickable</span>: <span class="hljs-literal">true</span>,
});
</code></pre>
<p>The above code is valid if we are working directly on the <a target="_blank" href="https://codesandbox.io/p/sandbox/p3f7rh?file=%2Findex.html%3A58%2C33">index.html</a> file and we're using the <code>swiper</code> and <code>swiper-slide</code> classes on div elements. </p>
<p>For Swiper Custom Elements, you can write the same code as follows: 👇</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
    👉  <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span> <span class="hljs-attr">slides-per-view</span>=<span class="hljs-string">"3"</span> <span class="hljs-attr">space-between</span>=<span class="hljs-string">"20"</span> <span class="hljs-attr">scrollbar-clickable</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">mousewheel-invert</span>=<span class="hljs-string">"true"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide1<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide2<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide3<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide4<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide5<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>All Swiper parameters are written in the form of <a target="_blank" href="https://www.freecodecamp.org/news/snake-case-vs-camel-case-vs-pascal-case-vs-kebab-case-whats-the-difference/#kebab-case">kebab-case</a> attributes on the <code>&lt;swiper-container&gt;</code> Custom Elements.</p>
<p>Parameters with <em>subkeys</em> and <em>values</em> are written as a single attribute and value. For example, <code>scrollbar-clickable=”true”</code> is a parameter with subkey <em>(clickable)</em> now written as a single attribute with a value. </p>
<p>If you run the code above, you should get the same result as below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Parameter_as_value.gif" alt="swiper.js sliders with three-column views" width="600" height="400" loading="lazy"></p>
<p>From the browser out, we’ve been able to split the view to <em>three-column</em> views instead of the <em>one-column</em> view we previously had. We were able to do this using the <code>slide-per-view</code> attribute parsed to the <code>&lt;swiper-container&gt;</code> Custom Element. For the full available list of parameters, see the <a target="_blank" href="https://swiperjs.com/swiper-api#parameters">Swiper APIs</a>.</p>
<h2 id="heading-responsive-breakpoints-in-swiper-element">Responsive Breakpoints in Swiper Element</h2>
<p>Swiper Custom Element requires attributes to parse parameter objects. Where complex object properties are required in the case of breakpoints, we can use <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify">JSON.stringify()</a> to convert objects to strings, making the breakpoints object properties readable values to the <code>&lt;swiper-container&gt;</code>.</p>
<p>Consider the code example below:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span> <span class="hljs-attr">breakpoints</span>=<span class="hljs-string">{</span>
            <span class="hljs-attr">JSON.stringify</span>({

                <span class="hljs-attr">640:</span>{
                    <span class="hljs-attr">slidesPerView:</span> <span class="hljs-attr">1</span>,
                    <span class="hljs-attr">spaceBetween:</span> <span class="hljs-attr">20</span>,
                },

                <span class="hljs-attr">768:</span> {
                    <span class="hljs-attr">slidesPerView:</span> <span class="hljs-attr">3</span>,
                    <span class="hljs-attr">spaceBetween:</span> <span class="hljs-attr">40</span>,
                },

                <span class="hljs-attr">1024:</span> {
                    <span class="hljs-attr">slidesPerView:</span> <span class="hljs-attr">4</span>,
                    <span class="hljs-attr">spaceBetween:</span> <span class="hljs-attr">50</span>,
                }
            })
        }&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide1<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide2<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide3<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide4<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide5<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide6<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide7<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide8<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide9<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Here, we are adding three different breakpoints <em><code>640</code></em>, <code>768</code>, and <code>1024</code> respectively for three different screen sizes (media queries), where <code>640</code> means (<code>max-width:640px</code>) for small screens. </p>
<p>Your code should look like this when you run it on the browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Responsive_Breakpoints.gif" alt="Responsive Breakpoints with Swiper Element" width="600" height="400" loading="lazy"></p>
<p>Keep in mind that breakpoints respond as soon as the components <a target="_blank" href="https://www.freecodecamp.org/news/how-to-understand-a-components-lifecycle-methods-in-reactjs-e1a609840630/">mount</a> (that is, React renders the components for the first time). So if you happen to resize your Desktop browser while learning this, you will need to reload the entire page by clicking on the reload icon to see the breakpoints changes. </p>
<h2 id="heading-pagination-and-navigation">Pagination and Navigation</h2>
<p>Swiper comes with <code>navigation</code> and <code>pagination</code> parameters for adding controls to sliders. With <code>navigation</code>, you can move from the current slide to the previous or next slide, while with <code>pagination</code> you can jump to a specific slide within the slider container. </p>
<p>To enable this on the swiper custom element, apply each parameter as an attribute on the <code>&lt;swiper-container&gt;</code> with a value of <code>true</code>. </p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span> <span class="hljs-attr">space-between</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">slides-per-view</span>=<span class="hljs-string">"3"</span> <span class="hljs-attr">pagination</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">navigation</span>=<span class="hljs-string">"true"</span> &gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide1<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide2<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide3<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide4<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>Slide5<span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your code should look just like this on the browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/pagination_and_navigation.gif" alt="pagination and navigation in Swiper Element" width="600" height="400" loading="lazy"></p>
<p>To move the slides, click on the navigation arrow or pagination bullet points. </p>
<h2 id="heading-lazy-loading-images">Lazy Loading Images</h2>
<p>We can implement <a target="_blank" href="https://www.freecodecamp.org/news/how-to-lazy-load-images-in-react/">lazy loading</a> on image sliders in Swiper Custom Elements. Apply the <code>lazy=”true”</code> and <code>loading=”lazy”</code> as attributes on the <code>&lt;swiper-slide&gt;</code> and the <code>&lt;img /&gt;</code> tag, respectively. </p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{</span>
        {
        "<span class="hljs-attr">--swiper-navigation-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>", 
        "<span class="hljs-attr">--swiper-pagination-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>"
        }
    } 
    <span class="hljs-attr">pagination-clickable</span>=<span class="hljs-string">"true"</span> 
    <span class="hljs-attr">navigation</span>=<span class="hljs-string">"true"</span> 
    <span class="hljs-attr">className</span>=<span class="hljs-string">"mySwiper"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> <span class="hljs-attr">lazy</span>=<span class="hljs-string">"true"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/slightly-opened-silver-macbook-mP7aPSUm7aE"</span> <span class="hljs-attr">loading</span>=<span class="hljs-string">"lazy"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> <span class="hljs-attr">lazy</span>=<span class="hljs-string">"true"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">loading</span>=<span class="hljs-string">"lazy"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/macbook-y0_vFxOHayg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> <span class="hljs-attr">lazy</span>=<span class="hljs-string">"true"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">loading</span>=<span class="hljs-string">"lazy"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/black-macbook-near-black-iphone-7-plus-and-black-apple-watch-HY3l4IeOc3E"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> <span class="hljs-attr">lazy</span>=<span class="hljs-string">"true"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">loading</span>=<span class="hljs-string">"lazy"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/apple-products-on-table-tdMu8W9NTnY"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> <span class="hljs-attr">lazy</span>=<span class="hljs-string">"true"</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">loading</span>=<span class="hljs-string">"lazy"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/turned-on-ipad-Im8ylpB8SpI"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Implementing lazy loading on images will require the lazy <em>preloader</em> element to be added to each slide on the <code>&lt;swiper-slide&gt;</code> component when <code>lazy="true"</code> is set on the element.</p>
<p>I added some CSS styles to clean things up a bit, so you can just copy the below code:</p>
<pre><code class="lang-css">*,*<span class="hljs-selector-pseudo">::before</span>, *<span class="hljs-selector-pseudo">::after</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">box-sizing</span>: border-box;
    <span class="hljs-attribute">font-family</span>: calibri;
}

<span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">justify-content</span>: center;
}

<span class="hljs-selector-tag">swiper-container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">400px</span>;

}

<span class="hljs-selector-tag">swiper-slide</span> {
   <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
}

<span class="hljs-selector-tag">swiper-slide</span> <span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">object-fit</span>: cover;
}
</code></pre>
<p>Your code should look like this when you run it on the browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/lazy_loading_images.gif" alt="lazy loading images in Swiper Element" width="600" height="400" loading="lazy"></p>
<h2 id="heading-effects-in-swiper-element">Effects in Swiper Element</h2>
<p>You can add effects in Swiper Element by setting the <code>effect</code> attribute to the desired value you need for your project. For example, we implemented a <code>cube</code> effect to create a 3D-like slide display. </p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{</span>
        {
        "<span class="hljs-attr">--swiper-navigation-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>", 
        "<span class="hljs-attr">--swiper-pagination-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>"
        }
    } 

    <span class="hljs-attr">pagination-clickable</span>=<span class="hljs-string">"true"</span> 
    <span class="hljs-attr">navigation</span>=<span class="hljs-string">"true"</span>
    <span class="hljs-attr">effect</span>=<span class="hljs-string">"cube"</span>
    <span class="hljs-attr">grab-cursor</span>=<span class="hljs-string">"true"</span>
    <span class="hljs-attr">cube-effect-shadow</span>=<span class="hljs-string">"true"</span>
    <span class="hljs-attr">cube-effect-slide-shadows</span>=<span class="hljs-string">"true"</span>
    <span class="hljs-attr">cube-effect-shadow-offset</span>=<span class="hljs-string">"20"</span>
    <span class="hljs-attr">cube-effect-shadow-scale</span>=<span class="hljs-string">"0.94"</span>

    <span class="hljs-attr">className</span>=<span class="hljs-string">"mySwiper"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> &gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/slightly-opened-silver-macbook-mP7aPSUm7aE"</span>  <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/macbook-y0_vFxOHayg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/black-macbook-near-black-iphone-7-plus-and-black-apple-watch-HY3l4IeOc3E"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/apple-products-on-table-tdMu8W9NTnY"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/turned-on-ipad-Im8ylpB8SpI"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your code should look like this on the browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/effects.gif" alt="cube effect in Swiper Element" width="600" height="400" loading="lazy"></p>
<p>Here, we apply the cube effect's attributes as follows:</p>
<ul>
<li><strong>effect=”cube”:</strong> sets the slider to a cube</li>
<li><strong>grab-cursor=”true”:</strong> changes the cursor to a grabbable icon.</li>
<li><strong>cube-effect-shadow=”true”</strong>: sets the shadow to the main slide component</li>
<li><strong>cube-effect-slide-shadows=”true”:</strong> sets the shadow to the slide element</li>
<li><strong>cube-effect-shadow-offset=”true”</strong>: sets the offset direction of the shadow</li>
<li><strong>cube-effect-shadow-scale</strong>: sets the size of the shadow</li>
</ul>
<p>Aside from the cube effect, you can specify other values as you can find listed in the <a target="_blank" href="https://swiperjs.com/swiper-api#cube-effect">Swiper API here</a>.</p>
<h2 id="heading-vertical-sliders">Vertical Sliders</h2>
<p>To apply vertical slides, simply apply the <code>direction="vertical”</code> attribute on the <code>&lt;swiper-component&gt;</code> as you can see in the code below:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{</span>
        {
        "<span class="hljs-attr">--swiper-navigation-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>", 
        "<span class="hljs-attr">--swiper-pagination-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>"
        }
    } 

    <span class="hljs-attr">pagination-clickable</span>=<span class="hljs-string">"true"</span> 
    <span class="hljs-attr">navigation</span>=<span class="hljs-string">"true"</span>

    <span class="hljs-attr">direction</span>=<span class="hljs-string">"vertical"</span>

    <span class="hljs-attr">className</span>=<span class="hljs-string">"mySwiper"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> &gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/slightly-opened-silver-macbook-mP7aPSUm7aE"</span>  <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/macbook-y0_vFxOHayg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/black-macbook-near-black-iphone-7-plus-and-black-apple-watch-HY3l4IeOc3E"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your code should look like this when you run it on the browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/vertical_slides.gif" alt="vertical slides in Swiper Element" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-style-swiper-element">How to Style Swiper Element</h2>
<p>Styling Swiper Element is very straightforward. Swiper comes with lots of premade classes that are applied to Swiper parameters (Attributes in Swiper Custom Elements). We can overwrite the default style rules on these classes with the HTML <code>style</code> attribute or external CSS rules. </p>
<p>Swiper Custom Elements can be styled in two ways: you can reset the default styles on the <em>Swiper Custom Elements</em> and the <em>Custom CSS Classes</em>, or create a <em>new class name</em> on the Swiper Element and style it. </p>
<h3 id="heading-reset-swiper-custom-styles">Reset Swiper Custom Styles</h3>
<p>To reset the Swiper Custom Element styles, target the specific Swiper Custom Elements or classes that the default styles are applied to and change them to your desired styles.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{</span>
        {
        "<span class="hljs-attr">--swiper-navigation-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>", 
        "<span class="hljs-attr">--swiper-pagination-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>"
        }
    } 

    <span class="hljs-attr">pagination-clickable</span>=<span class="hljs-string">"true"</span> 
    <span class="hljs-attr">navigation</span>=<span class="hljs-string">"true"</span>

    <span class="hljs-attr">className</span>=<span class="hljs-string">"mySwiper"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> &gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/slightly-opened-silver-macbook-mP7aPSUm7aE"</span>  <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/macbook-y0_vFxOHayg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/black-macbook-near-black-iphone-7-plus-and-black-apple-watch-HY3l4IeOc3E"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>From the code sample above, the <code>--swiper-navigation-color</code> and <code>--swiper-pagination-color</code> are set to the <code>#fff</code> color to change the default colors on the <em>navigation</em> and <em>pagination</em> buttons from the <code>#007aff</code> light blue color. Depending on what you are working on, you can customize this further using other custom classes from the <a target="_blank" href="https://swiperjs.com/swiper-api">Swiper APIs</a>. </p>
<p>Additionally, the <code>&lt;swiper-container&gt;</code> and <code>&lt;swiper-slide&gt;</code> are styled as follows:</p>
<pre><code class="lang-jsx">*,*::before, *::after {
    <span class="hljs-attr">margin</span>: <span class="hljs-number">0</span>;
    padding: <span class="hljs-number">0</span>;
    box-sizing: border-box;
    font-family: calibri;
}

body {
    <span class="hljs-attr">display</span>: flex;
    justify-content: center;
}

swiper-container {
    <span class="hljs-attr">width</span>: <span class="hljs-number">800</span>px;
    height: <span class="hljs-number">400</span>px;

}

swiper-slide {
   <span class="hljs-attr">width</span>: <span class="hljs-number">100</span>%;
   height: <span class="hljs-number">100</span>%;
}

swiper-slide img {
    <span class="hljs-attr">width</span>: <span class="hljs-number">100</span>%;
    height: <span class="hljs-number">100</span>%;
    object-fit: cover;
}
</code></pre>
<p>From the code sample above, the <code>swiper-container</code> is set to <code>width: 800px</code> and <code>height: 400px</code> to give the <code>swiper-container</code> a defined <code>width</code> and <code>height</code>. The <code>swiper-slide</code> is set to <code>100%</code> on both <code>width</code> and <code>height</code> to intrinsically set any child element of the <code>swiper-slide</code> to the default size of the <code>swiper-container</code>. </p>
<p>Also, the <code>swiper-container</code> does not have a specific <code>width</code> and <code>height</code> set. This makes it take the default size of the content within the <code>swiper-slide</code>. </p>
<p>Also, note that the <code>swiper-container</code> and <code>swiper-slide</code> are styled as an actual HTML element without a period (<code>.</code>) for class or hash sign (<code>#</code>) for id.</p>
<p>Your code should look like this when you run it on your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/custom_styles.gif" alt="Styling Swiper Custom Element and Classes" width="600" height="400" loading="lazy"></p>
<h3 id="heading-create-a-new-class-name">Create a New Class Name</h3>
<p>Sometimes, a single page can contain multiple <code>&lt;swiper-container&gt;</code> that solve different problems, and each may require its own styles. Creating a new class name might be very useful to provide different styling options. </p>
<pre><code class="lang-jsx">&lt;swiper-container className=<span class="hljs-string">"image-swiper"</span> &gt;
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span> &gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/slightly-opened-silver-macbook-mP7aPSUm7aE"</span>  <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span></span>

            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/macbook-y0_vFxOHayg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span></span>
 &lt;/swiper-container&gt;
</code></pre>
<p>The <code>image-swiper</code> class indicates that the Swiper Element is an image slider.</p>
<h2 id="heading-how-to-build-a-custom-slider-element">How to Build a Custom Slider Element</h2>
<p>Swiper has default controls such as <em>navigation</em> and <em>pagination</em>, which makes it easy to control the slides within the <code>swiper-container</code>.  The navigation and pagination come with some default icons for user interaction. </p>
<p>For example, you have the <em>left</em> and <em>right</em> angle icons for navigation and small <em>bullet</em> icons for pagination. But based on your design or UI requirements for a specific website or application, this might need to change. </p>
<p>Below is a simple way of implementing custom SVGs as navigation icons and customizing the pagination for better user interaction. </p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"slider-main-container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">swiper-container</span>

            <span class="hljs-attr">navigation-next-el</span>=<span class="hljs-string">".custom-next-button"</span>
            <span class="hljs-attr">navigation-prev-el</span>=<span class="hljs-string">".custom-prev-button"</span>
            <span class="hljs-attr">pagination-clickable</span>=<span class="hljs-string">"true"</span>
            <span class="hljs-attr">pagination-dynamic-bullets</span>=<span class="hljs-string">"true"</span>
            <span class="hljs-attr">autoplay-delay</span>=<span class="hljs-string">"2000"</span>
            <span class="hljs-attr">autoplay-disable-on-interaction</span>=<span class="hljs-string">"false"</span>
            <span class="hljs-attr">center-slides</span>=<span class="hljs-string">"true"</span>

            <span class="hljs-attr">style</span>=<span class="hljs-string">{</span>
                {
                "<span class="hljs-attr">--swiper-pagination-color</span>"<span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>",
                "<span class="hljs-attr">--swiper-pagination-bullet-size</span>"<span class="hljs-attr">:</span> "<span class="hljs-attr">15px</span>",
                }
            }
        &gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/white-lamborghini-aventador-parked-in-building-7_OQMgoGzDw"</span>  <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/shallow-focus-photo-of-white-sedan-oN661Kw9ZOY"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/black-car-interior-4xM5cytsdMo"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/turned-on-monitor-inside-vehicle-tU-L__PI7gw"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">swiper-slide</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://source.unsplash.com/black-and-blue-vacuum-cleaner-rHfTdK9YU2Q"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-slide</span>&gt;</span>

            {/* Navigations */}
        <span class="hljs-tag">&lt;/<span class="hljs-name">swiper-container</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-btn custom-prev-button"</span>&gt;</span>
          {/* <span class="hljs-comment">&lt;!-- https://feathericons.dev/?search=arrow-left&amp;iconset=feather --&gt;</span> */}
          <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 24 24"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"24"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"24"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"main-grid-item-icon"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#fff"</span> <span class="hljs-attr">strokeLinecap</span>=<span class="hljs-string">"round"</span> <span class="hljs-attr">strokeLinejoin</span>=<span class="hljs-string">"round"</span> <span class="hljs-attr">strokeWidth</span>=<span class="hljs-string">"2"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"19"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"5"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"12"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"12"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">polyline</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"12 19 5 12 12 5"</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-btn custom-next-button"</span>&gt;</span>
             {/* <span class="hljs-comment">&lt;!-- https://feathericons.dev/?search=arrow-right&amp;iconset=feather --&gt;</span> */}
             <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span> <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 24 24"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"24"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"24"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"main-grid-item-icon"</span> <span class="hljs-attr">fill</span>=<span class="hljs-string">"none"</span> <span class="hljs-attr">stroke</span>=<span class="hljs-string">"#fff"</span> <span class="hljs-attr">strokeLinecap</span>=<span class="hljs-string">"round"</span> <span class="hljs-attr">strokeLinejoin</span>=<span class="hljs-string">"round"</span> <span class="hljs-attr">strokeWidth</span>=<span class="hljs-string">"2"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">line</span> <span class="hljs-attr">x1</span>=<span class="hljs-string">"5"</span> <span class="hljs-attr">x2</span>=<span class="hljs-string">"19"</span> <span class="hljs-attr">y1</span>=<span class="hljs-string">"12"</span> <span class="hljs-attr">y2</span>=<span class="hljs-string">"12"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">polyline</span> <span class="hljs-attr">points</span>=<span class="hljs-string">"12 5 19 12 12 19"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Here's what's going in in the above code:</p>
<ul>
<li><strong>navigation-next-el=".custom-next-button"</strong>: used to specify the custom element for next navigation icon.</li>
<li><strong>navigation-prev-el=".custom-prev-button"</strong>: used to specify the custom element for prev navigation icon.</li>
<li><strong>pagination-clickable="true"</strong>: enables and sets the pagination to a clickable button.</li>
<li><strong>pagination-dynamic-bullets="true"</strong>: used to the change design presentation of the pagination button.</li>
<li><strong>autoplay-delay="2000"</strong>: enables slides to automatically play themselves and repeat.</li>
<li><strong>center-slides="true"</strong>: sets the active slide to center.</li>
</ul>
<p>The SVG icons are declared outside the <code>swiper-container</code> element to prevent it from clipping to the first slide. The <code>&lt;main&gt;</code> tag is set to a class of <code>slider-wrapper</code> to wrap both the custom slider elements and the custom SVGs icons. </p>
<p>I added some CSS for styling. You can copy it here.</p>
<pre><code class="lang-css">*,*<span class="hljs-selector-pseudo">::before</span>, *<span class="hljs-selector-pseudo">::after</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">box-sizing</span>: border-box;
    <span class="hljs-attribute">font-family</span>: calibri;
}

<span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">justify-content</span>: center;
}

<span class="hljs-selector-tag">swiper-container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">400px</span>;

}

<span class="hljs-selector-class">.slider-main-container</span> {
    <span class="hljs-attribute">position</span>: relative;
}

<span class="hljs-selector-tag">swiper-slide</span> {
   <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
}

<span class="hljs-selector-tag">swiper-slide</span> <span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">object-fit</span>: cover;
}

<span class="hljs-selector-class">.nav-btn</span> {
    <span class="hljs-attribute">display</span>: block;
    <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">z-index</span>: <span class="hljs-number">20</span>;
    <span class="hljs-attribute">user-select</span>: none;
    <span class="hljs-attribute">-webkit-user-select</span>: none;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.3</span>);
    <span class="hljs-attribute">cursor</span>: pointer;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">2px</span>;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
    <span class="hljs-attribute">transition</span>: background <span class="hljs-number">0.5s</span> ease;
}

<span class="hljs-selector-class">.nav-btn</span><span class="hljs-selector-pseudo">:hover</span> {
    <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">255</span>, <span class="hljs-number">165</span>, <span class="hljs-number">0</span>);
}

<span class="hljs-selector-class">.nav-btn</span><span class="hljs-selector-class">.custom-prev-button</span> {
    <span class="hljs-attribute">top</span>: <span class="hljs-number">50%</span>;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">2em</span>;
}

<span class="hljs-selector-class">.nav-btn</span><span class="hljs-selector-class">.custom-next-button</span> {
    <span class="hljs-attribute">right</span>: <span class="hljs-number">2em</span>;
    <span class="hljs-attribute">top</span>: <span class="hljs-number">50%</span>;
}
</code></pre>
<p>Your code should look like this when you run it on the browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Custom_Slider_Elements.gif" alt="Customize Swiper Element in React" width="600" height="400" loading="lazy"></p>
<h2 id="heading-summary">Summary</h2>
<p>Swiper Element is a new way of creating sliders in Swiper.js from the new version 11.0.6. </p>
<p>In this tutorial, I explained how to create and implement the new Swiper Custom Element in a React Application.</p>
<p>You saw some use cases for Swiper.js such as navigation, pagination, slides, effects, and properties. You also learned how to customize Swiper from the default design to give it a more professional style.</p>
<p>Thank you for reading!</p>
<h3 id="heading-references">References</h3>
<ol>
<li><a target="_blank" href="https://swiperjs.com/">Official Swiper Docs</a></li>
<li><a target="_blank" href="https://swiperjs.com/element">Swiper Element Docs</a></li>
<li><a target="_blank" href="https://swiperjs.com/swiper-api">Swiper API reference</a></li>
<li><a target="_blank" href="https://swiperjs.com/demos">Swiper Project Demo</a></li>
<li><a target="_blank" href="https://github.com/nolimits4web/swiper/">Swiper Git Issues</a>s</li>
</ol>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Implement Dynamic Segments with useParams in React Router ]]>
                </title>
                <description>
                    <![CDATA[ On a traditional website, when a user clicks on a URL, the browser makes a full-page request from the server and directs the user to a new page. This is referred to as static routing. This is very useful if you just need to navigate the user to a new... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/use-dynamic-segments-in-react-router/</link>
                <guid isPermaLink="false">66c5f6921bceda0ea5c39781</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react router ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Alex Anie ]]>
                </dc:creator>
                <pubDate>Wed, 31 Jan 2024 22:34:08 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/01/Dynamic-Segment-in-react-router-800x418.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>On a traditional website, when a user clicks on a URL, the browser makes a full-page request from the server and directs the user to a new page. This is referred to as static routing.</p>
<p>This is very useful if you just need to navigate the user to a new page. But with the development of web apps, there's more need for pages to render on the client or load dynamically. </p>
<p>This involves updating particular parts of the URL called segments, as well as rendering new content or updating the content on the same page without making a request from the server or reloading the entire page. </p>
<p>This is very common and useful in modern web apps. It enables client-side rendering, improves website navigation, and enables smooth transitions and animations (since the browser doesn't need to reload the page from external servers).</p>
<p>This can overall improve website performance and enable a good user experience.</p>
<p>In this tutorial, you'll learn about Dynamic Segments in React Router.</p>
<p>We'll go over what Dynamic Routing is and how it’s different from Static Routing. We'll also cover how to use <em>useParams</em> to enable Dynamic Segments, and how to set your path when getting data from an API.</p>
<p>Lastly, we’ll build a new project that dynamically renders new content to the same page when the user clicks on the sidebar.</p>
<p>By the end of this guide, you should be able to implement dynamic segments on your own in your React app.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></li>
<li><a class="post-section-overview" href="#heading-project-setup">Project Setup</a></li>
<li><a class="post-section-overview" href="#heading-installations">Installations</a><ul>
<li><a class="post-section-overview" href="#heading-install-react">Install React</a></li>
<li><a class="post-section-overview" href="#heading-install-react-router">Install React Router</a></li>
<li><a class="post-section-overview" href="#heading-install-feather-icon">Install Feather Icon</a></li>
<li><a class="post-section-overview" href="#heading-install-tailwind-css">Install Tailwind CSS</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-client-side-routing">Client-Side Routing</a></li>
<li><a class="post-section-overview" href="#heading-dynamic-segments">Dynamic Segments</a></li>
<li><a class="post-section-overview" href="#heading-nested-routing">Nested Routing</a></li>
<li><a class="post-section-overview" href="#heading-project-build-an-art-gallery">Project: Build an Art Gallery</a><ul>
<li><a class="post-section-overview" href="#heading-project-overview">Project Overview</a></li>
<li><a class="post-section-overview" href="#heading-folder-structure">Folder Structure</a></li>
<li><a class="post-section-overview" href="#heading-how-to-set-up-the-home-page">How to Set Up the Home Page</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-and-style-the-navbar">How to Create and Style the Navbar</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-the-asidebar">How to Create the AsideBar</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-the-asidebar">How to Create the Content Component</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-summary">Summary</a></li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>To follow along, you'll need a basic knowledge of the following:</p>
<ul>
<li>React</li>
<li>React-Router</li>
<li>Tailwind CSS (optional)</li>
</ul>
<h2 id="heading-project-setup">Project Setup</h2>
<p>To get started, create a folder called <code>dynamic-segment</code> and open it in VS Code (or your code editor of choice):</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled.png" alt="VS code screeshot" width="600" height="400" loading="lazy"></p>
<p>Next, click on <code>Ctrl +</code> (backtick) to launch the terminal as indicated above. This will enable us to install the npm packages we’ll be using in this project. </p>
<h2 id="heading-installations">Installations</h2>
<p>Now that we have our project set up, let’s install the <code>npm</code> packages that we need to get our project up and running.</p>
<h3 id="heading-install-reacthttpsreactdev">Install <a target="_blank" href="https://react.dev/">React</a></h3>
<p>React is a JavaScript library for building reusable and interactive components. To install it, copy and paste the command provided by <a target="_blank" href="https://vitejs.dev/">vite.js</a> below. </p>
<pre><code class="lang-bash">npm create vite@latest
</code></pre>
<p>Then just follow the installing guide to finish the process. Once the installation is complete, the node_modules folder should be present in your project folder.</p>
<h3 id="heading-install-react-routerhttpsreactroutercomenmain">Install <a target="_blank" href="https://reactrouter.com/en/main">React Router</a></h3>
<p>This is a React routing library for creating client-side routing applications. To install it, copy and paste the command below and press enter.</p>
<pre><code class="lang-bash">npm i react-router-dom
</code></pre>
<h3 id="heading-install-feather-iconhttpsfeathericonsdev">Install <a target="_blank" href="https://feathericons.dev/">Feather Icon</a></h3>
<p>Feather icon is a small yet beautiful collection of 24 x 24 grid open-source icons. It's built for adding flat icons on web applications. </p>
<p>To install it, paste the command below and press enter. </p>
<pre><code class="lang-bash">npm i react-feather
</code></pre>
<h3 id="heading-install-tailwind-csshttpstailwindcsscom">Install <a target="_blank" href="https://tailwindcss.com/">Tailwind CSS</a></h3>
<p>Tailwind is a utility-first CSS framework for building beautiful and compact website designs. To install it, run the command on the terminal below. </p>
<pre><code class="lang-bash">npm install -D tailwindcss postcss autoprefixer
</code></pre>
<p>This will create a <code>tailwind.config.js</code> file. Next, generate your <code>postcss.config.js</code> files with the command below:</p>
<pre><code class="lang-bash">npx tailwindcss init -p
</code></pre>
<p>Next, configure your template paths and add the paths to all of your template files in your <code>tailwind.config.js</code> file. Then click <code>ctrl + s</code> to save.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">/** <span class="hljs-doctag">@type <span class="hljs-type">{import('tailwindcss').Config}</span> </span>*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
  <span class="hljs-attr">content</span>: [
    <span class="hljs-string">"./index.html"</span>,
    <span class="hljs-string">"./src/**/*.{js,ts,jsx,tsx}"</span>,
  ],
  <span class="hljs-attr">theme</span>: {
    <span class="hljs-attr">extend</span>: {},
  },
  <span class="hljs-attr">plugins</span>: [],
}
</code></pre>
<p>Next, delete all CSS styles in the <code>./src/index.css</code> file and add the Tailwind directives <code>@tailwind</code> for each of Tailwind’s layers.</p>
<pre><code class="lang-css"><span class="hljs-keyword">@tailwind</span> base;
<span class="hljs-keyword">@tailwind</span> components;
<span class="hljs-keyword">@tailwind</span> utilities;
</code></pre>
<p>Next, delete the <code>assets</code> folder, the <code>App.css</code> and <code>App.jsx</code> files from the <code>/src</code> folder. Once you’ve done that, configure the <code>main.jsx</code> files as the route component as indicated below:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;

<span class="hljs-keyword">import</span> {
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'text-blue-700'</span>&gt;</span>Hello, world<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>Next, run the below command in the terminal to launch your app:</p>
<pre><code class="lang-jsx">npm run dev
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled-1.png" alt="Hello, World in React" width="600" height="400" loading="lazy"></p>
<h2 id="heading-client-side-routing">Client-Side Routing</h2>
<p>In React Router, navigation is relative between the <code>path</code> attribute and the <code>to</code> property. When a user clicks using the <code>&lt;Link&gt;</code> component (<code>&lt;a&gt;</code> tag), it navigates to the specified <code>path</code> within the route component and renders the component when it matches.</p>
<p>This type of navigation is called client-side routing because we are not rendering the pages from the server, but rather navigating from one component to another within the app.</p>
<p>The example below explains how client siding routing works:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//main.jsx</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;
nt
<span class="hljs-keyword">import</span> 
{
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
<span class="hljs-keyword">import</span> Book <span class="hljs-keyword">from</span> <span class="hljs-string">'./book'</span>;
<span class="hljs-keyword">import</span> Bookshop <span class="hljs-keyword">from</span> <span class="hljs-string">'./bookshop'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>
     👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
     👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>From the code example above, we imported two components, <code>Book</code> and <code>Bookshop</code>, and link them via the <code>&lt;a&gt;</code> tag and the <code>Route</code> component on the <code>./src/main.jsx</code> component.</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//book.jsx</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Book</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"px-4"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>77 Ways get to more customers By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Ubuy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Authenticity By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Emanuel Rose<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span> Change Your thinking change your life By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Brian Tracy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>

            👉 <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"bookshop"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 inline-block px-4 underline"</span>&gt;</span>see bookshop<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                {/* <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"publisher/itemId"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 underline"</span>&gt;</span>Publisher<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span> */}
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>The <code>href</code> attribute accepts the <code>bookshop</code> component as a relative path. So clicking the link should navigate you to the <code>bookshop</code> component.</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//bookshop.jsx</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Bookshop</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"px-4"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>list of book shops<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Book Shop &amp; Stationery<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Simon books<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>Dynamic Book home<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>

  👉 <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 inline-block px-4 underline"</span>&gt;</span>Names of Books<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}
</code></pre>
<p>The <code>href=”/”</code> in the bookshop component specifies the index route and should navigate you back to the home components.</p>
<p>Your app should look like this on your browser – click to navigate to the bookshop component.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Browser_output.gif" alt="Client-Side Routing in React" width="600" height="400" loading="lazy"></p>
<p>From the browser output above, you'll notice how the URL is updated from the address bar and a new component is rendered. </p>
<p>The type of routing is called client-side routing and only updates the URL path once with the new route. </p>
<p>In the next section, I'll explain how you can update a particular segment and render content dynamically. </p>
<h2 id="heading-dynamic-segments">Dynamic Segments</h2>
<p>A Dynamic Segment, as the name suggests, is a way of rendering a new component (UI) by updating a particular segment in the URL called params. You use the <a target="_blank" href="https://reactrouter.com/en/main/hooks/use-params">useParams</a> hook from <code>react-router-dom</code> to do this. </p>
<p>This is very useful in situations where content needs to render dynamically from a particular component or third-party API.</p>
<p>Continuing from where we stopped in the code, go to the <code>./src/main.jsx</code> component. Edit the Route and add <code>:itemId</code> to the path as indicated below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//main.jsx</span>

&lt;Route&gt;
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>} /&gt;</span>
 👉  &lt;Route path=<span class="hljs-string">'publisher/:itemId'</span> element={<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Publisher</span> /&gt;</span></span>} /&gt;
    &lt;/Route&gt;
</code></pre>
<p>Note that the <code>:</code> in the <code>:itemId</code> URL segment means Dynamic Segment. </p>
<p>Next, create a new component as <code>./src/publisher.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//publisher.jsx</span>

<span class="hljs-keyword">import</span> { useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Publisher</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> { itemId } = useParams();

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    {
        itemId ? (
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Book publishing companies<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Penguin Random House<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Scholastic<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>LPI Media<span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        ) : (
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Page item is not present<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        )
    }
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Let's talk about what this code is doing: </p>
<ul>
<li><code>const { itemId } = useParams()</code>: here we apply <em>destructuring</em> to get the params from the URL in the address bar. With this, we can render the return content.</li>
<li><code>itemId?():</code>: here we conditionally render a list of bookshop companies when a clicked link matches the params.</li>
</ul>
<p>Next, in the <code>./src/book</code> component, include the <code>publisher/itemId</code> as indicated in the <code>&lt;a&gt;</code> tag below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//book.jsx</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Book</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"px-4"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>77 Ways get to more customers By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Ubuy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span>Authenticity By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Emanuel Rose<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">ol</span>&gt;</span> Change Your thinking change your life By: <span class="hljs-tag">&lt;<span class="hljs-name">i</span>&gt;</span>Brian Tracy<span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>

                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"bookshop"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 inline-block px-4 underline"</span>&gt;</span>see bookshop<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            👉  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"publisher/itemId"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-blue-600 underline"</span>&gt;</span>Publisher<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Dynamic_segment_one.gif" alt="Dynamic Segments in React" width="600" height="400" loading="lazy"></p>
<p>Notice the update in the URL at the browser address bar. </p>
<p>Let's look at another example.</p>
<p>In a real-world application, Dynamic Segments are mostly used to render content dynamically when the segment <code>:itemId</code> matches the <code>id</code> of the returned APIs.  </p>
<p>Let’s see how this works. First, we need to decide where we will be fetching our data from. In this case, create an external JavaScript object <code>./scr/books.js</code> and copy and paste the code below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//books.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> [
    {   <span class="hljs-attr">id</span>: <span class="hljs-string">"1"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"The Great Gatsby"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"F. Scott Fitzgerald"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1925"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"The Great Gatsby is a 1925 novel by American writer F. Scott Fitzgerald. Set in the Jazz Age on Long Island, near New York City, the novel depicts first-person narrator Nick Carraway's interactions with mysterious millionaire Jay Gatsby and Gatsby's obsession to reunite with his former lover, Daisy Buchanan."</span>
    },

    {   <span class="hljs-attr">id</span>: <span class="hljs-string">"2"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"Pride and Prejudice"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Jane Austen"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1813"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"Pride and Prejudice is the second novel by English author Jane Austen, published in 1813. A novel of manners, it follows the character development of Elizabeth Bennet, the protagonist of the book"</span>
    },

    {   <span class="hljs-attr">id</span>: <span class="hljs-string">"3"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"To Kill a Mockingbird"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Harper Lee"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1960"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"To Kill a Mockingbird is a novel by the American author Harper Lee. It was published in June 1960 and became instantly successful. In the United States"</span>
    },

    {   <span class="hljs-attr">id</span>:<span class="hljs-string">"4"</span>,
        <span class="hljs-attr">title</span>: <span class="hljs-string">"Beloved"</span>,
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Toni Morrison"</span>,
        <span class="hljs-attr">year</span>: <span class="hljs-string">"1987"</span>,
        <span class="hljs-attr">description</span>: <span class="hljs-string">"Beloved is a 1987 novel by American novelist Toni Morrison. Set in the period after the American Civil War, the novel tells the story of a dysfunctional family of formerly enslaved people whose Cincinnati home is haunted by a malevolent spirit"</span>
    }
]
</code></pre>
<p>Next, create a new component called <code>./src/FavBooks.jsx</code> and write in the code below:</p>
<pre><code class="lang-jsx">⚠️ <span class="hljs-comment">//FavBooks.js</span>

<span class="hljs-keyword">import</span> { useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
<span class="hljs-keyword">import</span> book <span class="hljs-keyword">from</span> <span class="hljs-string">'./book'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FavBooks</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> {bookId} =  useParams() 

👉 <span class="hljs-keyword">const</span> newFavBook = book.find(<span class="hljs-function">(<span class="hljs-params">book</span>) =&gt;</span> book.id === bookId)

  <span class="hljs-keyword">if</span>(!newFavBook){
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`This page doesn't contain fav Books`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>
  }
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
          {newFavBook &amp;&amp; (
            <span class="hljs-tag">&lt;&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`Title: ${newFavBook.title}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`By: ${newFavBook.author}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`Year: ${newFavBook.year}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{`Description: ${newFavBook.description}`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
            <span class="hljs-tag">&lt;/&gt;</span>
          )}
      <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>Next, go to the <code>./src/book.jsx</code> component and update the code as follows:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;
<span class="hljs-keyword">import</span> books <span class="hljs-keyword">from</span> <span class="hljs-string">'./book.js'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Books</span>(<span class="hljs-params"></span>) </span>{

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-3xl"</span>&gt;</span>{`List of my favourite books`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span> 
        {
          books &amp;&amp; books.map((book)=&gt; (
            <span class="hljs-tag">&lt;&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">newbooks</span>/${<span class="hljs-attr">book.id</span>}`} <span class="hljs-attr">className</span>=<span class="hljs-string">'text-blue-600 underline'</span>&gt;</span>{book.title}<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
          <span class="hljs-tag">&lt;/&gt;</span>
          )) 
        }
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>Next, configure the <code>path</code> to dynamic segment on the route component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Book <span class="hljs-keyword">from</span> <span class="hljs-string">'../src/books'</span>;
<span class="hljs-keyword">import</span> Bookshop <span class="hljs-keyword">from</span> <span class="hljs-string">'./bookshop'</span>;
<span class="hljs-keyword">import</span> Publisher <span class="hljs-keyword">from</span> <span class="hljs-string">'./publisher'</span>;
👉 <span class="hljs-keyword">import</span> FavBooks <span class="hljs-keyword">from</span> <span class="hljs-string">'./FavBooks'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>}&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>} /&gt;
            <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'publisher/:itemId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Publisher</span> /&gt;</span>} /&gt;
     👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'newbooks/:bookId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">FavBooks</span> /&gt;</span>} /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Dynamic_segment_from_api.gif" alt="Dynamic segment from api in react" width="600" height="400" loading="lazy"></p>
<p>From the browser output, the URL params are being updated with the path segment and the id values from the books.js object. </p>
<p>Try clicking on each of the titles and notice how the id of the books.js object is present in the URL.</p>
<p>When a user clicks the link, it renders a new UI on a new page. But there are cases where you might want to render the content of the API on the same page as list items, so the content doesn’t have to open on a new page. To do this, we have to implement nested routing. </p>
<h2 id="heading-nested-routing">Nested Routing</h2>
<p>Nested routing makes it possible to nest routes to render new components on the same page for easy navigation and quick interactivity of the element. Nested routes make listed items function as a tab. As soon as any tab is clicked, the content that matches the corresponding tab gets displayed.</p>
<p>Now let’s see how to convert our little application to a nested route. </p>
<p>To go the <code>./src/main</code> component and create a nested route as follows:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Book</span> /&gt;</span>} &gt;
   👉  <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'newbooks/:bookId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">FavBooks</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'bookshop'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Bookshop</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'publisher/:itemId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Publisher</span> /&gt;</span>}&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)
</code></pre>
<p>Essentially, we are nesting the <code>FavBook</code> component to the <code>Book</code> component as its direct child, so the content will display below it.</p>
<p>Next, create a div tag and render an outlet component. This is us telling react-router where to render the newly nested route. </p>
<pre><code class="lang-jsx"><span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-3xl"</span>&gt;</span>{`List of my favourite books`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    👉 <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'flex'</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'m-4'</span>&gt;</span> 
          {
            books &amp;&amp; books.map((book)=&gt; (
              <span class="hljs-tag">&lt;&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">newbooks</span>/${<span class="hljs-attr">book.id</span>}`} <span class="hljs-attr">className</span>=<span class="hljs-string">'text-blue-600 underline'</span>&gt;</span>{book.title}<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;/&gt;</span>
            )) 
          }
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'w-[70%]'</span>&gt;</span>
       👉  <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
</code></pre>
<p>Note that to render the nested <code>FavBook</code> component side by side, both the outlet and the book list tag are nested in a section tag, and a style of display flex is applied.</p>
<p>Your code should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/rendered_outlet.gif" alt="Nested Routing in React" width="600" height="400" loading="lazy"></p>
<p>From the browser output, you can see that each listed item functions as a tab, and clicking them renders out the content of the API.</p>
<p>You've learned how to create a dynamic segment. In the next section, we'll build a project to help cement what we’ve learned even further.</p>
<h2 id="heading-project-build-an-art-gallery">Project: Build an Art Gallery</h2>
<p>In this project, we are going to build an art gallery app that contains a list of sculpture and art from different countries. This will help you solidify the concepts you've previously learned. </p>
<p>We are going to implement the following features:</p>
<ul>
<li>Client Side Routing</li>
<li>Active Links</li>
<li>Dynamic Segments</li>
<li>Nested Routes</li>
</ul>
<p>Below is a quick overview of what the project will look like.</p>
<h3 id="heading-project-overview">Project Overview</h3>
<p>Here is a complete preview of our project after completion. You can download the source code on 👉 <a target="_blank" href="https://github.com/alex-anie/Arts-Culture-dynamic-segment-example">GitHub</a> here.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/cotent_component-1.gif" alt="Project: Build an Art Gallery in React.js" width="600" height="400" loading="lazy"></p>
<h3 id="heading-folder-structure">Folder Structure</h3>
<p>This is what the project's folder structure should look like:</p>
<pre><code class="lang-html">📂src
        📂apis
            ├──data.js
        📂components
            ├──AsideBar.jsx
            ├──Content.jsx
            ├──Navbar.jsx
        📂pages
            ├──home.jsx
    ├──index.css
    ├──main.jsx
├──index.html
</code></pre>
<h3 id="heading-how-to-set-up-the-home-page">How to Set Up the Home Page</h3>
<p>To set up the home page, create a home component <code>./src/pages/home.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">├──home.jsx

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-orange-600"</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Next, go to the <code>main.jsx</code> component. If you don’t have one yet, create it as <code>./src/main.jsx.</code> then configure the route as follows:</p>
<pre><code class="lang-jsx">├──main.jsx

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;

<span class="hljs-keyword">import</span> {
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">'react-router-dom'</span>;

<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./pages/home'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>  <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>}&gt;

    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>Next, type <code>npm run dev</code> to launch your app. </p>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled-2.png" alt="Hello, World in React" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-create-and-style-the-navbar">How to Create and Style the Navbar</h3>
<p>Now that we have the home and route component set up, let’s create the navbar component, which is the top component in our app.</p>
<p>Create a component <code>./src/components/navbar.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">├──navbar.jsx

<span class="hljs-keyword">import</span> { Activity, Search } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-feather"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Navbar</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex justify-between bg-slate-200 rounded-3xl py-2"</span>&gt;</span>
                {/* logo */}
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Activity</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"inline-block ml-10 mr-2 text-orange-500"</span> /&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"inline-block text-xl"</span>&gt;</span>{`Arts &amp; Culture`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

                {/* Navlinks */}
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-white rounded-3xl py-1 px-2 mr-5"</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">Search</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"inline-block mr-1 text-slate-500"</span>/&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"search"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"site-search"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"q"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Search anything"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-transparent outline-none text-slate-800"</span>/&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
   <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>From the code above, the navbar is divided between the logo and the search bar.</p>
<ul>
<li><strong>The logo:</strong> we import the <em>Activity Icons</em> as a component from the feather icons and apply some Tailwind CSS classes to style it. The icon is set to <code>inline-block</code> so that we can apply space to it. We apply <code>ml-10</code> and <code>mr-2</code>, which is margin-left of <code>2.5rem</code> and right <code>0.5rem</code> with a color of orange (<code>text-orange-500</code>).</li>
<li><strong>The search</strong>: for the search bar, we also imported it from feather icons as a component and applied the following styling: <code>inline-block mr-1 text-slate-500</code>. If you are having a hard time understanding Tailwind CSS classes you can read more about them from the docs <a target="_blank" href="https://tailwindcss.com/">here</a>.</li>
</ul>
<p>To position the logo and search bar side by side, we set the parent header to display flex and justify-content of space-between to apply space in between the logo and search bar.</p>
<p>Next, add the navbar to the route as follows:</p>
<pre><code class="lang-jsx">├──main.jsx

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom/client'</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>

<span class="hljs-keyword">import</span> {
  RouterProvider,
  createBrowserRouter,
  createRoutesFromElements,
  Route
} <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>

👉  <span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Navbar'</span>
        <span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./pages/home'</span>

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>  <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>}&gt;
    👉   <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Navbar</span> /&gt;</span>} /&gt; 
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>From the code above, the Navbar component is nested inside the Home components. This means we have to use an Outlet component to render the Navbar component.</p>
<p>Next, go to the <code>./src/page/home</code> component, import, and replace the <code>&lt;p&gt;</code> with the Outlet component as indicated below:</p>
<pre><code class="lang-jsx">├──home.jsx

👉 <span class="hljs-keyword">import</span> { Outlet } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
           👉 <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your app should look like this on your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/Untitled-3.png" alt="Navbar component in react" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-create-the-asidebar">How to Create the AsideBar</h3>
<p>The <code>AsideBar</code> is one of the most important components of our app. This is where the names of the cultures will be displayed. This component functions as a tab, and when a user clicks on it, it renders out more details about the culture that was clicked on. </p>
<p>Create a new component as <code>./src/components/AsideBar.jsx</code> and write in the code below:</p>
<pre><code class="lang-jsx">├──AsideBar.jsx

<span class="hljs-keyword">import</span> { NavLink } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>
<span class="hljs-keyword">import</span> data <span class="hljs-keyword">from</span> <span class="hljs-string">"../apis/data"</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AsideBar</span>(<span class="hljs-params"></span>) </span>{

    <span class="hljs-keyword">const</span> activeStyle = <span class="hljs-function">(<span class="hljs-params">{isActive}</span>) =&gt;</span>  {
            <span class="hljs-keyword">return</span> {
                <span class="hljs-attr">backgroundColor</span> : isActive ? <span class="hljs-string">"rgb(154 52 18)"</span> : <span class="hljs-string">""</span>,
                <span class="hljs-attr">color</span> : isActive ? <span class="hljs-string">"rgb(255 247 237)"</span> : <span class="hljs-string">""</span>,
            }
        }

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[100%] mt-[2em]"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[100%]"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[fit-content] bg-slate-200 rounded-xl"</span>&gt;</span>
                    {
                        data.map((data)=&gt;(
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{data.id}</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[100%] py-3 px-2 inline-block text-slate-800 hover:bg-orange-200 transition-all whitespace-nowrap border-y-4 "</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">content</span>/${<span class="hljs-attr">data.id</span>}`} <span class="hljs-attr">style</span>=<span class="hljs-string">{activeStyle}</span>&gt;</span>
                                        {data.type}
                                    <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        ))
                    }
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>From the code example above, we have our code split into two sections: the <code>data</code> and the <code>NavLink</code> component.</p>
<ul>
<li><strong>The data:</strong> we imported the data from <code>./src/apis/data.js</code> and we are mapping each array of objects and returning the <code>data.type</code> as the names of the <code>AsideBar</code>.</li>
<li><strong>The NavLink</strong>: the data returned from <code>data.js</code> is rendered directly on the <code>NavLink</code> component. The <code>NavLink</code> component has two props specified, the <code>style</code> and <code>to</code> props. The <code>style</code> prop received the <code>activeStyle</code> object that indicated what style should be applied to <code>NavLink</code> when its active. The <code>to</code> prop <code>to={</code>content/${data.id}<code>}</code>  we pass the <code>data.id</code> as a segment to match with the <code>path</code> of the content components (<em>more on this in the next section</em>). This enables the content to dynamically render when the <code>NavLink</code> is clicked on.</li>
</ul>
<p>Next, go to the home component and render the <code>AsideBar</code> as indicated below:</p>
<pre><code class="lang-jsx">├──home.jsx

      <span class="hljs-keyword">import</span> { Outlet } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
👉 <span class="hljs-keyword">import</span> AsideBar <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/AsideBar"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      👉  <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[80%] mt-[2em] mx-auto"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span>&gt;</span>
                 👉 <span class="hljs-tag">&lt;<span class="hljs-name">AsideBar</span> /&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Your app should look like this in your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/aside_component.gif" alt="aside component in react" width="600" height="400" loading="lazy"></p>
<p>From interacting with the <code>Asidebar</code>, you may have noticed that the page breaks any time you click on the links. This is because the content component is not defined yet. So let’s create it.</p>
<h3 id="heading-how-to-create-the-content-component">How to Create the Content Component</h3>
<p>The content component renders the content that is related to a particular link that was clicked. </p>
<p>Create a new component called <code>./src/components/Content.jsx</code> and add the code below:</p>
<pre><code class="lang-jsx">├──Content.jsx

<span class="hljs-keyword">import</span> { Link, useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
<span class="hljs-keyword">import</span> data <span class="hljs-keyword">from</span> <span class="hljs-string">"../apis/data.js"</span>;
<span class="hljs-keyword">import</span> { WifiOff } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-feather"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Content</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> {contentId} = useParams()

    <span class="hljs-keyword">const</span> newData = data.find(<span class="hljs-function">(<span class="hljs-params">data</span>)=&gt;</span> data.id.toString() === contentId)

    <span class="hljs-keyword">if</span>(!contentId){
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"translate-x-44 translate-y-44"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">WifiOff</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-slate-400 text-center translate-x-48"</span>/&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-slate-400"</span>&gt;</span>{`Content can't be accessed! click the left nav to reload`}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
        )
    }

<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[80%] mx-auto mt-8"</span>&gt;</span> 
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span> &gt;</span>
                {
                    newData &amp;&amp; (
                        <span class="hljs-tag">&lt;&gt;</span>  
                            {/* Image Over */}
                            <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"h-[6em] w-[100%]"</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"h-[100%]  w-[100%]"</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{newData.imgHeaders}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"h-[100%]  w-[100%] object-cover rounded-xl"</span>/&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>

                            {/* Details */}
                           <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-6"</span>&gt;</span>
                           <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[50%]"</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-orange-500 w-[fit-content] rounded-xl mt-4 py-1 px-2 font-bold"</span>&gt;</span>{newData.catagories}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-light text-4xl my-7"</span>&gt;</span>{newData.type}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-bold mb-4 text-2xl"</span>&gt;</span>{newData.region}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-light"</span>&gt;</span>{newData.history}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"mt-4"</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>{`Learn more from`}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{newData.britannicaLink}</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-orange-500 py-2 px-2 rounded-md inline mt-4 hover:underline hover:text-black"</span>&gt;</span>britannica<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>

                            {/* Image Cover */}
                            <span class="hljs-tag">&lt;<span class="hljs-name">aside</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[50%]"</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{newData.imgCover}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"rounded-3xl mt-10"</span>/&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>

                        <span class="hljs-tag">&lt;/&gt;</span>
                    )
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
</code></pre>
<p>The code above is doing the following:</p>
<ul>
<li><strong>useParams</strong>: we use the <a target="_blank" href="https://reactrouter.com/en/main/hooks/use-params">useParams()</a> hook to return the key and value pairs of the dynamic segment <code>content/:contentId</code> specified on the route.</li>
<li><strong>newData</strong>: using the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find">find()</a> array method, the first element of the object array is returned if the condition is true, otherwise it returns <code>undefined</code>.</li>
<li><strong>if(!contentId)</strong>: here, we are checking to see if the <code>contentId</code> doesn’t match or is not rendered yet – then the provided element within the function should run. This is very useful for checking errors and in situations where the content is not available.</li>
<li><strong>newData &amp;&amp;</strong>: here we are mapping through the return data object and rendering the content of the API as soon as the content is loaded. Each object property is parsed into an element to render as content.</li>
</ul>
<p>Next, go to the home component and render the content component as indicated below.</p>
<pre><code class="lang-jsx">├──home.jsx

        <span class="hljs-keyword">import</span> { Outlet } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
        <span class="hljs-keyword">import</span> AsideBar <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/AsideBar"</span>;
👉 <span class="hljs-keyword">import</span> Content <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/Content"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-[80%] mt-[2em] mx-auto"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">section</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">Outlet</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>

            <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">AsideBar</span> /&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>

                <span class="hljs-tag">&lt;<span class="hljs-name">aside</span>&gt;</span>
              👉 <span class="hljs-tag">&lt;<span class="hljs-name">Content</span> /&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">aside</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
)
}
</code></pre>
<p>Next, configure the route to a dynamic segment:</p>
<pre><code class="lang-jsx">├──home.jsx

     <span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">'./pages/home'</span>;
     <span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/navbar'</span>;
👉 <span class="hljs-keyword">import</span> Content <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Content'</span>;

<span class="hljs-keyword">const</span> router = createBrowserRouter(
  createRoutesFromElements(
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>  <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>}&gt;
       <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'/'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Navbar</span> /&gt;</span>}&gt; 
       👉 <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">'content/:contentId'</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Content</span> /&gt;</span>} /&gt;
      <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Route</span>&gt;</span></span>
  )
)

ReactDOM.createRoot(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)).render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">RouterProvider</span> <span class="hljs-attr">router</span>=<span class="hljs-string">{router}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
)
</code></pre>
<p>Your app should look just like this on your browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/cotent_component-1-1.gif" alt="cotent component in react.js" width="600" height="400" loading="lazy"></p>
<p>Clicking on the <code>Asidebar</code> will load the content from the API and render it on the same page as the <code>Asidebar</code>.</p>
<h2 id="heading-summary">Summary</h2>
<p>In this tutorial, we learned about Dynamic Segments in React Router. We talked about what Dynamic Router is and how it’s different from static routing. You also learned how to use the <code>useParams</code> hook to enable Dynamic Segments, as well as how to set your path when getting data from an API</p>
<p>Then we built a new project that dynamically rendered new content to the same page when the user clicks on the sidebar.</p>
<p>You can take this project further and make it your own by implementing more features. </p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
