<?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[ react-navigation - 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[ react-navigation - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 24 Aug 2026 19:20:13 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/react-navigation/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ React Navigation – How to Build a Breadcrumb Component ]]>
                </title>
                <description>
                    <![CDATA[ I know what you're thinking – but despite the article title and cover image, this article isn't about bread or even pastries. Instead, it looks at a commonly used component in web applications for navigation called the breadcrumb component. Here, we'... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-navigation-build-a-breadcrumb-component/</link>
                <guid isPermaLink="false">66bb8914b0d3ac3d7acde3dd</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react-navigation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ routing ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tailwind ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Jaja ]]>
                </dc:creator>
                <pubDate>Mon, 25 Mar 2024 22:33:05 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/03/Breadcrumb-article-cover.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>I know what you're thinking – but despite the article title and cover image, this article isn't about bread or even pastries. Instead, it looks at a commonly used component in web applications for navigation called the breadcrumb component.</p>
<p>Here, we'll unravel the mysteries of breadcrumb trails in React applications. We'll dissect their types, and you'll learn how to seamlessly integrate them into your web projects for better user navigation.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li>Fundamentals of CSS and TailwindCSS</li>
<li>Fundamentals of ES6 JavaScript and React </li>
<li>Fundamentals of Routing and the React Router library ( check out this <a target="_blank" href="https://www.freecodecamp.org/news/improve-user-experience-in-react-by-animating-routes-using-framer-motion/">routing article</a> if you’re unfamiliar).</li>
</ul>
<h2 id="heading-what-well-cover">What We'll Cover:</h2>
<ol>
<li><a class="post-section-overview" href="#heading-understanding-breadcrumbs">Understanding Breadcrumbs</a></li>
<li><a class="post-section-overview" href="#heading-types-of-breadcrumb-navigation">Types of Breadcrumb Navigation</a></li>
<li><a class="post-section-overview" href="#heading-how-to-build-the-breadcrumb-component-in-react">How to Build the Breadcrumb Component in React</a></li>
<li><a class="post-section-overview" href="#heading-how-to-create-the-breadcrumb-component-structure">How to Create the Breadcrumb Component Structure</a><br>– <a class="post-section-overview" href="#heading-location-based-breadcrumbs">Location-Based Breadcrumbs</a><br>– <a class="post-section-overview" href="#heading-path-based-breadcrumbs">Path-Based Breadcrumbs</a><br>– <a class="post-section-overview" href="#heading-attribute-based-breadcrumbs">Attribute-Based Breadcrumbs</a></li>
<li><a class="post-section-overview" href="#heading-best-practices-for-breadcrumbs-in-react">Best Practices for Breadcrumbs in React</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ol>
<h2 id="heading-understanding-breadcrumbs">Understanding Breadcrumbs</h2>
<p>Before we venture deeper into the intricacies of breadcrumbs, let's set the scene. Imagine the classic tale of <a target="_blank" href="https://en.wikipedia.org/wiki/Hansel_and_Gretel">Hansel and Gretel</a>, where they leave a trail of breadcrumbs to find their way back home through the dense forest. </p>
<p>In the digital realm, breadcrumbs serve a similar purpose, albeit with a twist.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Hansel-And-Gretel-2.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Hansel And Gretel</em></p>
<p>Breadcrumbs, in the context of web navigation, are a series of hierarchical links typically displayed at the top of a webpage. These links reflect the user's path from the homepage to the current page, allowing them to retrace their steps or navigate to higher-level pages.</p>
<p>These navigation aids have a fascinating history and a crucial role in guiding users through a digital space.</p>
<p>A typical example of what this component looks like is shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Breadcrumb-example.png" alt="Image" width="600" height="400" loading="lazy">
<em>Breadcrumb example: Home Page &gt; Products Page &gt; Single Product Page (current page)</em></p>
<h3 id="heading-types-of-breadcrumb-navigation">Types of Breadcrumb Navigation</h3>
<ul>
<li><strong>Location-based Breadcrumbs</strong>: Like landmarks in a forest, location-based breadcrumbs show users where they are within the website's hierarchy. They show the current page's position relative to other pages on the site.</li>
<li><strong>Path-based Breadcrumbs</strong>: Like retracing your steps in the forest, path-based breadcrumbs display the user's journey through the website. They show the sequence of pages visited, helping users understand how they arrived at the current page.</li>
<li><strong>Attribute-based Breadcrumbs</strong>: These breadcrumbs highlight specific attributes or characteristics of the current page. They offer more context to the user's navigation, akin to discovering unique features along a trail.</li>
</ul>
<h2 id="heading-how-to-build-the-breadcrumb-component-in-react">How to Build the Breadcrumb Component in React</h2>
<p>The first step in this section involves creating a React environment. Before you begin, make sure to install <a target="_blank" href="https://nodejs.org/en/download">Node.js</a> on your computer if you don't have it already.</p>
<h3 id="heading-how-to-set-up-a-react-environment">How to Set Up a React Environment</h3>
<p>After installing Node.js, use <a target="_blank" href="https://vitejs.dev/guide/">Vite</a> (a modern build tool for React projects) to create a new React project. In your local terminal, run the command:</p>
<pre><code class="lang-bash">npm create vite@latest
</code></pre>
<p>Select React as your framework and your preferred variant.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Setting-up-a-react-environment-with-Vite.png" alt="Image" width="600" height="400" loading="lazy">
<em>Setting up a react environment with Vite</em></p>
<p>To install the necessary packages, run <code>npm install</code> and open it in your IDE.</p>
<p>Finally, clear the boilerplate code and start up your server using the command <code>npm run dev</code>.</p>
<p>This project will use Tailwind for styling. To get that set up, run the following command:</p>
<pre><code class="lang-bash">npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
</code></pre>
<p>After this command, a <code>tailwind.config.js</code> file will be created. Head into the config file, delete its content, and paste this in there instead:</p>
<pre><code class="lang-js"><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>Then, open your <code>index.css</code> file and paste the style configs (preferably at the top):</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>Finally, restart your dev server to get access to Tailwind.</p>
<h2 id="heading-how-to-create-the-breadcrumb-component-structure">How to Create the Breadcrumb Component Structure</h2>
<p>Rather than building one breadcrumb component, we’re going to build all three types mentioned above so you can see how they work.</p>
<h3 id="heading-location-based-breadcrumbs">Location-Based Breadcrumbs</h3>
<p>This type is the most basic type of breadcrumb that involves showing all the routes readily available for the user to navigate.</p>
<p>To begin, start by creating a Breadcrumb file and pasting in these styles:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> SlashImg <span class="hljs-keyword">from</span> <span class="hljs-string">"./assets/slash.png"</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">Breadcrumb</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">"bg-white "</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" flex border p-2 gap-6 text-xl text-[#2E4053] items-center"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md"</span>&gt;</span>
          Home
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md transition-all duration-300"</span>&gt;</span>
          Products
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md transition-all duration-300"</span>&gt;</span>
          About
        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md transition-all duration-300"</span>&gt;</span>
          FAQ
        <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">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>Then import this file into a <code>Home</code> component which you also need to create:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Breadcrumb <span class="hljs-keyword">from</span> <span class="hljs-string">"./Breadcrumb"</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;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" h-[100dvh] bg-gray-200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex flex-col items-center gap-8 "</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" text-4xl text-[#2E4053 ] mt-20"</span>&gt;</span>
          My Breadcrumb Component 🍞
        <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Breadcrumb</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>
  );
}
</code></pre>
<p>At the moment, your component looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Breadcrumb-after-rendering-on-UI.png" alt="Image" width="600" height="400" loading="lazy">
<em>Breadcrumb after rendering on UI</em></p>
<p>To perform navigation functionalities with this component, start by installing <a target="_blank" href="https://www.npmjs.com/package/react-router-dom">React Router</a> (a widely used library for managing navigation and routing in React applications).</p>
<pre><code class="lang-bash">npm i react-router-dom
</code></pre>
<p>Then create the routes in your App component.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { BrowserRouter, Navigate, Route, Routes } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">import</span> About <span class="hljs-keyword">from</span> <span class="hljs-string">"./About"</span>;
<span class="hljs-keyword">import</span> FAQ <span class="hljs-keyword">from</span> <span class="hljs-string">"./FAQ"</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">"./Home"</span>;
<span class="hljs-keyword">import</span> Homepage <span class="hljs-keyword">from</span> <span class="hljs-string">"./Homepage"</span>;
<span class="hljs-keyword">import</span> Products <span class="hljs-keyword">from</span> <span class="hljs-string">"./Products"</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">App</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">BrowserRouter</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">index</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Navigate</span> <span class="hljs-attr">replace</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"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">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">"home"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Homepage</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">"products"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Products</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">"about"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">About</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">"faq"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">FAQ</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">Routes</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
  );
}
</code></pre>
<p>The code block above is a configuration for client-side routing in a React application using React Router v6. It sets up a <code>BrowserRouter</code> to handle dynamic routing and defines a series of Route components within Routes to map URL paths to React components.</p>
<ul>
<li><code>BrowserRouter</code> is a router implementation that uses the HTML5 history API to keep the UI in sync with the URL.</li>
<li><code>Navigate</code> redirects users to a specific route. In this case, it redirects from the index route to <code>/home</code>.</li>
<li><code>Route</code> components define a mapping between a path and a component. The <code>element</code> prop specifies what to render when the path matches the current URL.</li>
<li>The <code>path="/" element={&lt;Home /&gt;}</code> route is a nested route that serves as a layout for its child routes. It renders the Home component when the URL is <code>/</code>. Nested inside the <code>Home</code> route are routes for <code>home</code>, <code>products</code>, <code>about</code>, and <code>faq</code>, each rendering their respective components when their path matches the URL.</li>
</ul>
<p>Next, head over to your Breadcrumb component and change the list elements to <code>Link</code> elements (imported from React Router) to aid routing between routes.</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> SlashImg <span class="hljs-keyword">from</span> <span class="hljs-string">"./assets/slash.png"</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">Breadcrumb</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">"bg-white "</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" flex border p-2 gap-6 text-xl text-[#2E4053] items-center"</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">home</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md"</span>&gt;</span>
          Home
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">products</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md transition-all duration-300"</span>&gt;</span>
          Products
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">about</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md transition-all duration-300"</span>&gt;</span>
          About
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">faq</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">" cursor-pointer hover:bg-[#E8DAEF] p-4 rounded-md transition-all duration-300"</span>&gt;</span>
          FAQ
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</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>
  );
}
</code></pre>
<p>Then use the <code>Outlet</code> component provided by React Router to display the content of each route in the <code>Home</code> component.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Breadcrumb <span class="hljs-keyword">from</span> <span class="hljs-string">"./Breadcrumb"</span>;
<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;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" h-[100dvh] bg-gray-200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex flex-col items-center gap-8 "</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" text-4xl text-[#2E4053 ] mt-20"</span>&gt;</span>
          My Breadcrumb Component 🍞
        <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Breadcrumb</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">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>Testing out your component in the browser now gives the following result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Routing-with-the-Breadcrumb-component.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Routing with the Breadcrumb component</em></p>
<p>With this, your location-based breadcrumbs are functional, but we can take it a step further. To improve the UX, we can add an active class to the currently active route, creating a visual indicator of where the user is at every point in time.</p>
<p>Start by extracting the current location of the user in the Breadcrumb component:</p>
<pre><code class="lang-jsx">  <span class="hljs-keyword">const</span> location = useLocation();
</code></pre>
<p>Then use the pathname property to add an active class to each link:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { Link, useLocation } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">import</span> SlashImg <span class="hljs-keyword">from</span> <span class="hljs-string">"./assets/slash.png"</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">Breadcrumb</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> location = useLocation();
  <span class="hljs-built_in">console</span>.log(location.pathname);

  <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">"bg-white "</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" flex border p-2 gap-6 text-xl text-[#2E4053] items-center"</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">home</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">hover:bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> ${
            <span class="hljs-attr">location.pathname</span> === <span class="hljs-string">"/home"</span> &amp;&amp; "<span class="hljs-attr">bg-</span>[#<span class="hljs-attr">b572d6</span>] <span class="hljs-attr">text-white</span>"
          }`}&gt;</span>
          Home
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">products</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">hover:bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> ${
            <span class="hljs-attr">location.pathname</span> === <span class="hljs-string">"/products"</span> &amp;&amp; "<span class="hljs-attr">bg-</span>[#<span class="hljs-attr">b572d6</span>] <span class="hljs-attr">text-white</span>"
          }`}&gt;</span>
          Products
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">about</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">hover:bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> ${
            <span class="hljs-attr">location.pathname</span> === <span class="hljs-string">"/about"</span> &amp;&amp; "<span class="hljs-attr">bg-</span>[#<span class="hljs-attr">b572d6</span>] <span class="hljs-attr">text-white</span>"
          }`}&gt;</span>
          About
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{SlashImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">faq</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">hover:bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> ${
            <span class="hljs-attr">location.pathname</span> === <span class="hljs-string">"/faq"</span> &amp;&amp; "<span class="hljs-attr">bg-</span>[#<span class="hljs-attr">b572d6</span>] <span class="hljs-attr">text-white</span>"
          }`}&gt;</span>
          FAQ
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</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>
  );
}
</code></pre>
<p>This now gives the following result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Routing-with-the-Breadcrumb-component-after-adding-an-active-class.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Routing with the Breadcrumb component after adding an active class</em></p>
<p>Tasty! 🍩</p>
<h3 id="heading-path-based-breadcrumbs">Path-Based Breadcrumbs</h3>
<p>This breadcrumb type uses the pattern of progressive reveal to better guide the users on where they are based on their actions.</p>
<p>Here we’re going to create two routes and move from the first to the third (Home to Single product page).</p>
<p>Start by modifying your <code>Home</code> component a bit.</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">Home</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">" h-[100dvh] bg-gray-200"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex flex-col items-center gap-8 "</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" text-4xl text-[#2E4053 ] mt-20"</span>&gt;</span>
          My Breadcrumb Component 🍞
        <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Breadcrumb</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> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-4 p-2 "</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"products"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" rounded-md p-2 bg-[#777] text-white"</span>&gt;</span>
            Products
          <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Link</span>
            <span class="hljs-attr">to</span>=<span class="hljs-string">"products/1"</span>
            <span class="hljs-attr">className</span>=<span class="hljs-string">" rounded-md p-2 bg-[#777] text-white"</span>&gt;</span>
            Single Product
          <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">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>The changes include creating a single product page which we'll route to later.</p>
<p>Then make a nested route path for the page in the App component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { BrowserRouter, Navigate, Route, Routes } <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">"./Home"</span>;
<span class="hljs-keyword">import</span> Homepage <span class="hljs-keyword">from</span> <span class="hljs-string">"./Homepage"</span>;
<span class="hljs-keyword">import</span> Products <span class="hljs-keyword">from</span> <span class="hljs-string">"./Products"</span>;
<span class="hljs-keyword">import</span> SingleProduct <span class="hljs-keyword">from</span> <span class="hljs-string">"./SingleProduct"</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">App</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">BrowserRouter</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">index</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Navigate</span> <span class="hljs-attr">replace</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"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">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">"home"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Homepage</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">"products"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Products</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">":productId"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">SingleProduct</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 class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
  );
}
</code></pre>
<p>For the already existing Products page, add these styles and changes:</p>
<pre><code class="lang-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">Products</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">"bg-[#EDBB99] p-2 w-96 h-96 flex flex-col items-center"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Products Page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</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>
  );
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Page-after-creating-the-Single-Product-page-without-any-routing-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>Page after creating the Single Product page without any routing</em></p>
<p>Finally, modify your breadcrumb component to display the routes when you route to them from the home page.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { Link, useLocation } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">import</span> RightArrowImg <span class="hljs-keyword">from</span> <span class="hljs-string">"./assets/right-icon.png"</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">Breadcrumb</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> location = useLocation();

  <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">"bg-white "</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" flex border p-2 gap-6 text-xl text-[#2E4053] items-center"</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">home</span>"}
          <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">hover:bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">hover:text-black</span> <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> ${
            <span class="hljs-attr">location.pathname</span> === <span class="hljs-string">"/home"</span> &amp;&amp; "<span class="hljs-attr">bg-</span>[#<span class="hljs-attr">b572d6</span>] <span class="hljs-attr">text-white</span>"
          }`}&gt;</span>
          Home
        <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
        {location.pathname.includes("/products") &amp;&amp; (
          <span class="hljs-tag">&lt;&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{RightArrowImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">products</span>"}
              <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>` <span class="hljs-attr">hover:text-black</span> <span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">hover:bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> ${
                <span class="hljs-attr">location.pathname.includes</span>("/<span class="hljs-attr">products</span>") &amp;&amp;
                " <span class="hljs-attr">bg-</span>[#<span class="hljs-attr">b572d6</span>] <span class="hljs-attr">text-white</span>"
              } ${
                <span class="hljs-attr">location.pathname.includes</span>("/<span class="hljs-attr">products</span>/") &amp;&amp;
                " <span class="hljs-attr">bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">text-black</span>"
              }`}&gt;</span>
              Products
            <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
          <span class="hljs-tag">&lt;/&gt;</span>
        )}
        {location.pathname.includes(`/products/`) &amp;&amp; (
          <span class="hljs-tag">&lt;&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{RightArrowImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">products</span>"}
              <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">hover:text-black</span>  <span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">hover:bg-</span>[#<span class="hljs-attr">E8DAEF</span>] <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> ${
                <span class="hljs-attr">location.pathname.includes</span>("/<span class="hljs-attr">products</span>") &amp;&amp;
                "<span class="hljs-attr">bg-</span>[#<span class="hljs-attr">b572d6</span>] <span class="hljs-attr">text-white</span>"
              }`}&gt;</span>
              Single Product
            <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
          <span class="hljs-tag">&lt;/&gt;</span></span>
        )}
      &lt;/ul&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>In the code above, we’re displaying more breadcrumbs based on the route we’re in and applying styles to reflect the route changes.</p>
<p>Testing our component now gives the following result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Routing-with-the-Breadcrumb-component-after-adding-a-nested-route.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Routing with the Breadcrumb component after adding a nested route</em></p>
<p>Sweet! 🍬</p>
<h3 id="heading-attribute-based-breadcrumbs">Attribute-Based Breadcrumbs</h3>
<p>Attribute-based breadcrumbs focus on highlighting specific attributes or characteristics of the current page, such as tags, categories, or any other relevant metadata.</p>
<p>Instead of simply showing the user's path through the website hierarchy, they provide additional context that can aid in navigation and understanding. </p>
<p>A common use case for them is on E-commerce sites where you go through multiple items and filter through multiple product properties to find your desired product.</p>
<p>To begin, our app component is going to look drastically different with no routing done.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Products <span class="hljs-keyword">from</span> <span class="hljs-string">"./Products"</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">App</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">" h-[100dvh] bg-[#EDBB99]"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Products</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>Then add this JSX to your <code>Products</code> component:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> Breadcrumb <span class="hljs-keyword">from</span> <span class="hljs-string">"./Breadcrumb"</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">Products</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> dogsArray = [
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"S"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/small-white-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Gigi"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">1</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"M"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/medium-white-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Tom"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"L"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/big-white-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Jake"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"S"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/small-black-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Hill"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">1</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"M"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/medium-black-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Jack"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"L"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/big-black-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Jones"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"S"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"brown"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/small-brown-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Herbert"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">1</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"M"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"brown"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/medium-brown-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Coco"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>,
    },
    {
      <span class="hljs-attr">size</span>: <span class="hljs-string">"L"</span>,
      <span class="hljs-attr">color</span>: <span class="hljs-string">"brown"</span>,
      <span class="hljs-attr">image</span>: <span class="hljs-string">"/big-brown-dog.jpg"</span>,
      <span class="hljs-attr">name</span>: <span class="hljs-string">"Benny"</span>,
      <span class="hljs-attr">age</span>: <span class="hljs-number">3</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">"flex flex-col items-center p-2"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"p-4"</span>&gt;</span>Adopt Page<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Breadcrumb</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"relative grid grid-cols-5 gap-6"</span>&gt;</span>
          {dogsArray.map((dog) =&gt; (
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{dog.name}</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-[225px] rounded-md overflow-hidden"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-full "</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{dog.image}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">"grid items-center grid-cols-2 gap-2 mt-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center gap-2"</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Name:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white bg-orange-900 border rounded-[4px] p-1.5 min-w-14"</span>&gt;</span>
                    {dog.name}
                  <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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Size:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white min-w-14"</span>&gt;</span>{dog.size}<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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Color:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white capitalize min-w-14"</span>&gt;</span>
                    {dog.color}
                  <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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Age:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white min-w-14"</span>&gt;</span>
                    {`${dog.age + " " + "year"}${dog.age &gt; 1 ? "s" : ""}`}
                  <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">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">"absolute bottom-0 left-0 p-1 translate-y-[110%]"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"mb-2"</span>&gt;</span>Filter by<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center mb-4"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-12"</span>&gt;</span>Size:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">bg-white</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">min-w-14</span> `}&gt;</span>
                  All
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">bg-white</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">min-w-14</span> `}&gt;</span>
                  S
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">bg-white</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">min-w-14</span> `}&gt;</span>
                  M
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">bg-white</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">min-w-14</span> `}&gt;</span>
                  L
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</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">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-12"</span>&gt;</span>Color:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">bg-white</span>  <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">min-w-14</span> `}&gt;</span>
                  All
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">bg-white</span>  <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">min-w-14</span>`}&gt;</span>
                  White
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">bg-white</span> <span class="hljs-attr">min-w-14</span> `}&gt;</span>
                  Brown
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">p-2</span> <span class="hljs-attr">text-center</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">bg-white</span>  <span class="hljs-attr">min-w-14</span> `}&gt;</span>
                  Black
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</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">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">main</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>This JSX uses dummy dog data to create a template and style it with tailwind.</p>
<p>At the moment, your app should look like this;</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Page-after-looping-over-the-dummy-dog-data.png" alt="Image" width="600" height="400" loading="lazy">
<em>Page after looping over the dummy dog data</em></p>
<p>To implement the attribute-based breadcrumbs, start by creating two states for the attributes you want to filter by:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> [sizeFilter, setSizeFilter] = useState(<span class="hljs-literal">null</span>);
<span class="hljs-keyword">const</span> [colorFilter, setColorFilter] = useState(<span class="hljs-literal">null</span>);
</code></pre>
<p>Then create a function to filter based on the value passed in:</p>
<pre><code class="lang-jsx">  <span class="hljs-keyword">const</span> filteredDogs = dogsArray.filter(<span class="hljs-function">(<span class="hljs-params">dog</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (sizeFilter &amp;&amp; dog.size !== sizeFilter) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;

    <span class="hljs-keyword">if</span> (colorFilter &amp;&amp; dog.color !== colorFilter) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;

    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  });
</code></pre>
<p>After that, change the array you used used to create the JSX to the array returned from the function:</p>
<pre><code class="lang-jsx"> {filteredDogs.map(<span class="hljs-function">(<span class="hljs-params">dog</span>) =&gt;</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{dog.name}</span>&gt;</span></span>
</code></pre>
<p>Finally, use the setter function to pass in the values you want to filter by:</p>
<pre><code class="lang-jsx">&lt;main&gt;
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"relative grid grid-cols-5 gap-6"</span>&gt;</span>
          {filteredDogs.map((dog) =&gt; (
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{dog.name}</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-[225px] rounded-md overflow-hidden"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-full "</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{dog.image}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">"grid items-center grid-cols-2 gap-2 mt-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center gap-2"</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Name:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white bg-orange-900 border rounded-[4px] p-1.5 min-w-14"</span>&gt;</span>
                    {dog.name}
                  <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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Size:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white min-w-14"</span>&gt;</span>{dog.size}<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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Color:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white capitalize min-w-14"</span>&gt;</span>
                    {dog.color}
                  <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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Age:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white min-w-14"</span>&gt;</span>
                    {`${dog.age + " " + "year"}${dog.age &gt; 1 ? "s" : ""}`}
                  <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">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">"absolute bottom-0 left-0 p-1 translate-y-[110%]"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"mb-2"</span>&gt;</span>Filter by<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center mb-4"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-12"</span>&gt;</span>Size:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Reset</span> <span class="hljs-attr">size</span> <span class="hljs-attr">state</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter(null)}
                  className={`p-2 text-center bg-white rounded-md min-w-14 `}&gt;
                  All
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Set</span> <span class="hljs-attr">filter</span> <span class="hljs-attr">to</span> <span class="hljs-attr">small</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter("S")}
                  className={`p-2 text-center bg-white rounded-md min-w-14 `}&gt;
                  S
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Set</span> <span class="hljs-attr">filter</span> <span class="hljs-attr">to</span> <span class="hljs-attr">medium</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter("M")}
                  className={`p-2 text-center bg-white rounded-md min-w-14 `}&gt;
                  M
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Set</span> <span class="hljs-attr">filter</span> <span class="hljs-attr">to</span> <span class="hljs-attr">large</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter("L")}
                  className={`p-2 text-center bg-white rounded-md min-w-14 `}&gt;
                  L
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</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">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-12"</span>&gt;</span>Color:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Reset</span> <span class="hljs-attr">color</span> <span class="hljs-attr">state</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter(null)}
                  className={`p-2 text-center bg-white  rounded-md min-w-14 `}&gt;
                  All
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Set</span> <span class="hljs-attr">color</span> <span class="hljs-attr">to</span> <span class="hljs-attr">white</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter("white")}
                  className={`p-2 text-center bg-white  rounded-md min-w-14 `}&gt;
                  White
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Set</span> <span class="hljs-attr">color</span> <span class="hljs-attr">to</span> <span class="hljs-attr">brown</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter("brown")}
                  className={`p-2 text-center rounded-md bg-white min-w-14 `}&gt;
                  Brown
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  // <span class="hljs-attr">Set</span> <span class="hljs-attr">color</span> <span class="hljs-attr">to</span> <span class="hljs-attr">black</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter("black")}
                  className={`p-2 text-center rounded-md bg-white  min-w-14 `}&gt;
                  Black
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</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">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
      &lt;/main&gt;
</code></pre>
<p>Testing your component now gives the following result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Filtering-by-properties.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Filtering by properties working</em></p>
<p>Exquisite!🍦</p>
<p>To add our breadcrumb functionality, pass in the filter props to the component like this:</p>
<pre><code class="lang-jsx">&lt;Breadcrumb sizeFilter={sizeFilter} colorFilter={colorFilter} /&gt;
</code></pre>
<p>Then use those props to render the headers:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> RightArrowImg <span class="hljs-keyword">from</span> <span class="hljs-string">"./assets/right-icon.png"</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">Breadcrumb</span>(<span class="hljs-params">{ sizeFilter, colorFilter }</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">"mb-4 bg-gray-200 rounded-md "</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"   flex items-center  text-xl text-[#2E4053] text-left"</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>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> `}&gt;</span>All<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>

        {sizeFilter &amp;&amp; (
          <span class="hljs-tag">&lt;&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{RightArrowImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">capitalize</span>`}&gt;</span>
              {sizeFilter}
            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;/&gt;</span>
        )}
        {colorFilter &amp;&amp; (
          <span class="hljs-tag">&lt;&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{RightArrowImg}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-5 h-5 "</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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>`<span class="hljs-attr">cursor-pointer</span> <span class="hljs-attr">p-4</span> <span class="hljs-attr">rounded-md</span> <span class="hljs-attr">capitalize</span>`}&gt;</span>
              {colorFilter}
            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
          <span class="hljs-tag">&lt;/&gt;</span></span>
        )}
      &lt;/ul&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>Before we see the final result, let’s add indicators of what filter props are currently active:</p>
<pre><code class="lang-jsx">&lt;main&gt;
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"relative grid grid-cols-5 gap-6"</span>&gt;</span>
          {filteredDogs.map((dog) =&gt; (
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{dog.name}</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-[225px] rounded-md overflow-hidden"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-full "</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{dog.image}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</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">"grid items-center grid-cols-2 gap-2 mt-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center gap-2"</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Name:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white bg-orange-900 border rounded-[4px] p-1.5 min-w-14"</span>&gt;</span>
                    {dog.name}
                  <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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Size:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white min-w-14"</span>&gt;</span>{dog.size}<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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Color:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white capitalize min-w-14"</span>&gt;</span>
                    {dog.color}
                  <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">"flex gap-2 "</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Age:<span class="hljs-tag">&lt;/<span class="hljs-name">span</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-center text-white min-w-14"</span>&gt;</span>
                    {`${dog.age + " " + "year"}${dog.age &gt; 1 ? "s" : ""}`}
                  <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">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">"absolute bottom-0 left-0 p-1 translate-y-[110%]"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"mb-2"</span>&gt;</span>Filter by<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center mb-4"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-12"</span>&gt;</span>Size:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter(null)}
                  className={`p-2 text-center  rounded-md min-w-14 ${
                    // Dynamic background color add
                    sizeFilter === null
                      ? "bg-orange-900 text-white"
                      : "bg-white text-black "
                  }`}&gt;
                  All
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter("S")}
                  className={`p-2 text-center  rounded-md min-w-14 ${
                    // Dynamic background color add
                    sizeFilter === "S"
                      ? "bg-orange-900 text-white"
                      : "bg-white text-black "
                  }`}&gt;
                  S
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter("M")}
                  className={`p-2 text-center  rounded-md min-w-14 ${
                    // Dynamic background color add
                    sizeFilter === "M"
                      ? "bg-orange-900 text-white"
                      : "bg-white text-black "
                  }`}&gt;
                  M
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setSizeFilter("L")}
                  className={`p-2 text-center  rounded-md min-w-14 ${
                    // Dynamic background color add
                    sizeFilter === "L"
                      ? "bg-orange-900 text-white"
                      : "bg-white text-black "
                  }`}&gt;
                  L
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</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">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"w-12"</span>&gt;</span>Color:<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex gap-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter(null)}
                  className={`p-2 text-center   rounded-md min-w-14 ${
                    // Dynamic background color add
                    colorFilter === null
                      ? "bg-orange-900 text-white"
                      : "bg-white text-black "
                  }`}&gt;
                  All
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter("white")}
                  className={`p-2 text-center   rounded-md min-w-14 ${
                    // Dynamic background color add
                    colorFilter === "white"
                      ? "bg-orange-900 text-white"
                      : "bg-white text-black "
                  }`}&gt;
                  White
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter("brown")}
                  className={`p-2 text-center rounded-md   min-w-14 ${
                    // Dynamic background color add
                    colorFilter === "brown"
                      ? "bg-orange-900 text-white"
                      : " bg-white text-black "
                  }`}&gt;
                  Brown
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
                  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setColorFilter("black")}
                  className={`p-2 text-center rounded-md    min-w-14 ${
                    // Dynamic background color add
                    colorFilter === "black"
                      ? "bg-orange-900 text-white"
                      : " bg-white text-black"
                  }`}&gt;
                  Black
                <span class="hljs-tag">&lt;/<span class="hljs-name">button</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">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
      &lt;/main&gt;
</code></pre>
<p>A final test on your component now gives this result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/Filtering-by-properties-tested.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Filtering via properties and showing it in the breadcrumb</em></p>
<p>And voilà! Your component filters perfectly, and also shows a useful breadcrumb to help users know what properties they’ve filtered by.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/03/bruce-almighty-jim-carrey.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Bruce Almighty Jim Carrey beautiful gif</em></p>
<h2 id="heading-best-practices-for-breadcrumbs-in-react">Best Practices for Breadcrumbs in React</h2>
<p>When implementing breadcrumbs in React, it's crucial to follow certain best practices to ensure a seamless user experience. Here are some key points to consider:</p>
<ol>
<li><strong>Consistency with React Routing</strong>: Breadcrumbs should align with the application's routing structure. You can do this by implementing dynamic breadcrumbs using a good routing library (React Router). By defining routes and generating an array of breadcrumbs based on the user's current route, you can ensure that the breadcrumb trail reflects the user's navigation path.</li>
<li><strong>Breadth and Depth of Breadcrumb Trails</strong>: Breadcrumb trails should represent the user's location within the application. This includes using a separator, such as a slash ("/"), to distinguish between different parts of the breadcrumb trail.</li>
<li><strong>Naming and Navigation</strong>: Breadcrumbs should be easy to understand and navigate. This involves using clear and descriptive names for each breadcrumb and ensuring that each breadcrumb link is clickable, leading the user to the appropriate page.</li>
<li><strong>Ensuring Accessibility</strong>: Breadcrumbs should be accessible to all users. This can be achieved by using the <code>aria-label</code> attribute to identify the breadcrumb trail as a navigation landmark. This makes it easier for users with assistive technologies to locate and navigate the breadcrumb trail.</li>
<li><strong>Customization and Ease-of-Use</strong>: When using a component for creating breadcrumbs, consider its customization options and ease of use. Look for components that provide useful defaults and allow for easy customization of texts, links, and separators.</li>
</ol>
<p>By adhering to these best practices, you can create effective and user-friendly breadcrumbs in your React applications. </p>
<p>Here are links to the repositories on GitHub:</p>
<ul>
<li><a target="_blank" href="https://github.com/Daiveedjay/React-breadcrumb-article-location-based">Location-based</a></li>
<li><a target="_blank" href="https://github.com/Daiveedjay/React-breadcrumb-article-path-based">Path-based</a></li>
<li><a target="_blank" href="https://github.com/Daiveedjay/React-breadcrumb-article-attribute-based">Attribute-based</a></li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Implementing breadcrumbs in React applications not only provides a navigational aid but also contributes to a seamless and intuitive user experience. Adhering to best practices enhances the usability and accessibility of applications.</p>
<p>Just as the aroma of freshly baked bread entices hungry people to come visit the bakery, a well-structured breadcrumb trail can entice users to explore and navigate an application with ease, ultimately improving user navigation and experience. And that's how the cookie crumbles, leaving a trail of delightful navigation in its wake.</p>
<h3 id="heading-contact-information">Contact Information</h3>
<p>Want to connect or contact me? Feel free to hit me up on the following:</p>
<ul>
<li>Twitter / X: <a target="_blank" href="https://twitter.com/JajaDavid8">@jajadavid8</a></li>
<li>LinkedIn: <a target="_blank" href="https://www.linkedin.com/in/david-jaja-8084251b4/">David Jaja</a></li>
<li>Email: Jajadavidjid@gmail.com  </li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Handle Navigation in React Native with react-navigation 5 ]]>
                </title>
                <description>
                    <![CDATA[ By Said Hayani React-navigation is the navigation library that comes to my mind when we talk about navigation in React Native.  I'm a big fan of this library and it's always the first solution I use to handle navigation in React Native. This is in pa... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/introducing-react-navigation-5/</link>
                <guid isPermaLink="false">66d460dd4bc8f441cb6df827</guid>
                
                    <category>
                        <![CDATA[ React navigation 5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Android ]]>
                    </category>
                
                    <category>
                        <![CDATA[ iOS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ mobile app development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ programming languages ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react hooks ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react-navigation ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 23 Mar 2020 19:24:43 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/03/react-navigation5-featured-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Said Hayani</p>
<p>React-navigation is the navigation library that comes to my mind when we talk about navigation in React Native. </p>
<p>I'm a big fan of this library and it's always the first solution I use to handle navigation in React Native. This is in part becausae it has an awesome and easy API and is very customizable. </p>
<p>I'm writing this article because version 5 just went from beta to stable. It comes with some feature changes and a new API design that provides a simple and different way to declare routes.</p>
<p>In this article, we are going to go through the new APIs and look at ways to use them in our applications.</p>
<blockquote>
<p>Originally published on <a target="_blank" href="https://saidhayani.com/Introducing-react-navigation-5/">saidhayani.com</a></p>
</blockquote>
<h2 id="heading-installing">Installing</h2>
<p>The way you install react-navigation has changed a little bet compared to previous versions (&gt;4.x):</p>
<pre><code class="lang-shell">// &gt; 4.x verions
yarn add react-navigation
</code></pre>
<p>Installing react-navigation 5 will look like this:</p>
<pre><code class="lang-shell">// yarn
yarn add @react-navigation/native
// npm
npm install @react-navigation/native
</code></pre>
<p>The latest versions of react-navigation use many third party library like <a target="_blank" href="https://github.com/software-mansion/react-native-gesture-handler">react-native-gesture-handler</a> for animation and handling transitions. So you always need to install those libraries.</p>
<pre><code class="lang-shell">// yarn
yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
// npm
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
</code></pre>
<h2 id="heading-dynamic-screens">Dynamic screens</h2>
<p>The new API introduces dynamism in initializing routes. Previously it was done statically - basically, we had to define our Routes in a config file.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// @flow</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> { createAppContainer, createSwitchNavigator } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-navigation"</span>;
<span class="hljs-keyword">import</span> { createStackNavigator } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-navigation-stack"</span>;


<span class="hljs-comment">/** ---------Screens----------- */</span>
<span class="hljs-comment">// import LaunchScreen from "../Containers/LaunchScreen";</span>
<span class="hljs-keyword">import</span> HomeScreen <span class="hljs-keyword">from</span> <span class="hljs-string">"../Containers/HomeScreen"</span>;

<span class="hljs-keyword">import</span> ProfileScreen <span class="hljs-keyword">from</span> <span class="hljs-string">"../Containers/ProfileScreen"</span>;
<span class="hljs-keyword">import</span> LoginScreen <span class="hljs-keyword">from</span> <span class="hljs-string">"../Containers/LoginScreen"</span>;






<span class="hljs-keyword">const</span> StackNavigator = createStackNavigator(
  {
    <span class="hljs-attr">initialRouteName</span>: <span class="hljs-string">"Home"</span>
  },
  {
    <span class="hljs-attr">Home</span>: {
      <span class="hljs-attr">screen</span>: HomeScreen
    },
     <span class="hljs-attr">Login</span>: {
      <span class="hljs-attr">screen</span>: LoginScreen,
      <span class="hljs-attr">headerMode</span>: <span class="hljs-string">"none"</span>,

    },
      <span class="hljs-attr">Profile</span>: {
      <span class="hljs-attr">screen</span>: ProfileScreen
    }



);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> createAppContainer(StackNavigator);
</code></pre>
<p>The new API comes with dynamic components. and made the navigation to be more dynamic.
The new way to declare the Routes will be much like the following.</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> { SafeAreaView, StyleSheet, View, Text, StatusBar } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-native"</span>

<span class="hljs-keyword">import</span> { NavigationContainer } <span class="hljs-keyword">from</span> <span class="hljs-string">"@react-navigation/native"</span>
<span class="hljs-keyword">import</span> { createStackNavigator } <span class="hljs-keyword">from</span> <span class="hljs-string">"@react-navigation/stack"</span>

<span class="hljs-keyword">const</span> App: <span class="hljs-function">() =&gt;</span> React$Node = <span class="hljs-function">() =&gt;</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">StatusBar</span> <span class="hljs-attr">barStyle</span>=<span class="hljs-string">"dark-content"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">SafeAreaView</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.containerStyle}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppNavigation</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">SafeAreaView</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  )
}
<span class="hljs-keyword">const</span> Stack = createStackNavigator()
<span class="hljs-keyword">const</span> AppNavigation = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">NavigationContainer</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Navigator</span> <span class="hljs-attr">initialRouteName</span>=<span class="hljs-string">"home"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"home"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{HomeScreen}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Stack.Navigator</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavigationContainer</span>&gt;</span></span>
  )
}
<span class="hljs-keyword">const</span> HomeScreen = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.containerStyle}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.title}</span>&gt;</span>Home Screen<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
  )
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/react-navigation5-demo.gif" alt="react-navigation5-demo" width="600" height="400" loading="lazy"></p>
<p>This new way is dynamic, simpler to use, and is kinda similar to react-router API.</p>
<h2 id="heading-dynamic-options">Dynamic options</h2>
<p>This has been the most requested feature by the community for a long time. I always had issues with the old method (static) and it was really hard to change the navigation behavior dynamically.</p>
<h3 id="heading-the-old-method-gt-lt-4x">The old method =&gt; &lt; 4.x</h3>
<p>With older versions of <a target="_blank" href="https://reactnavigation.org/">react-navigation</a> we had to define static options. And there was no way to change this dynamically.</p>
<pre><code class="lang-js">  <span class="hljs-keyword">static</span> navigationOptions = {
    <span class="hljs-attr">title</span>: <span class="hljs-string">"Sign In"</span>,
    <span class="hljs-attr">header</span>: <span class="hljs-literal">null</span>,
    <span class="hljs-attr">mode</span>: <span class="hljs-string">"modal"</span>,
    <span class="hljs-attr">headerMode</span>: <span class="hljs-string">"none"</span>
  };
</code></pre>
<h3 id="heading-the-new-method-version-5">The new method (version 5)</h3>
<p>React-navigation comes with a dynamic method which is quite simple. We can set the options to any screen using just <code>props</code>.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> AppNavigation = <span class="hljs-function">(<span class="hljs-params">{}</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> auth = {
    <span class="hljs-attr">authenticated</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">user</span>: {
      <span class="hljs-attr">email</span>: <span class="hljs-string">"user@mail.com"</span>,
      <span class="hljs-attr">username</span>: <span class="hljs-string">"John"</span>,
    },
  }
  <span class="hljs-keyword">let</span> ProfileScreenTitle = auth.authenticated ? auth.user.username : <span class="hljs-string">"Profile"</span>
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">NavigationContainer</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Navigator</span> <span class="hljs-attr">initialRouteName</span>=<span class="hljs-string">"Home"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Home"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{HomeScreen}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span>
          <span class="hljs-attr">name</span>=<span class="hljs-string">"Profile"</span>
          <span class="hljs-attr">component</span>=<span class="hljs-string">{ProfileScreen}</span>
          <span class="hljs-attr">options</span>=<span class="hljs-string">{{</span>
            <span class="hljs-attr">title:</span> <span class="hljs-attr">ProfileScreenTitle</span>,
            <span class="hljs-attr">headerTintColor:</span> "#<span class="hljs-attr">4aa3ba</span>",
            <span class="hljs-attr">headerStyle:</span> {
              <span class="hljs-attr">backgroundColor:</span> <span class="hljs-attr">darkModeOn</span> ? "#<span class="hljs-attr">000</span>" <span class="hljs-attr">:</span> "#<span class="hljs-attr">fff</span>",
            },
          }}
        /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Stack.Screen</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"About"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{AboutScreen}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Stack.Navigator</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavigationContainer</span>&gt;</span></span>
  )
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/react-navigation-header.png" alt="react-navigation-header" width="600" height="400" loading="lazy"></p>
<p>With dynamic options, I can change the title based on authentication. For example if the user is authenticated, I can set the screen title to be the user’s username, or I can change the backgroundColor for the header. </p>
<p>This is more useful especially if you are using dynamic themes or if you are willing to implement dark mode in your app.</p>
<h2 id="heading-hooks">Hooks</h2>
<p>This is my favorite feature so far, and it’s a time-saver. The new API introduced some custom hooks to perform certain actions.</p>
<p>In previous versions, for example, if I had to get the currentName of the active screen, I had to create some helpers to do that for me pretty much like the following.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getCurrentRouteName</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> | <span class="hljs-title">null</span> </span>{
  <span class="hljs-keyword">const</span> tag = <span class="hljs-string">"[getCurrentRouteNameSync] "</span>
  <span class="hljs-keyword">const</span> navState = getStore().getState().nav
  <span class="hljs-keyword">const</span> currentRoute = getActiveRouteState(navState)
  <span class="hljs-built_in">console</span>.log(tag + <span class="hljs-string">" currentRoute &gt; "</span>, currentRoute)
  <span class="hljs-keyword">return</span> currentRoute &amp;&amp; currentRoute.routeName ? currentRoute.routeName : <span class="hljs-literal">null</span>
}
</code></pre>
<p>The hooks API helps me avoid all these things and makes it easier for me to access the Navigation API with one single line using a hook.</p>
<p>Now I can easily get the RouteName using <code>useRoute</code> Hook.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { useRoute } <span class="hljs-keyword">from</span> <span class="hljs-string">"@react-navigation/native"</span>
<span class="hljs-keyword">const</span> AboutScreen = <span class="hljs-function">(<span class="hljs-params">{ navigation }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> route = useRoute()
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span>
      <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span>
        <span class="hljs-attr">justifyContent:</span> "<span class="hljs-attr">space-around</span>",
        <span class="hljs-attr">flex:</span> <span class="hljs-attr">1</span>,
        <span class="hljs-attr">alignItems:</span> "<span class="hljs-attr">center</span>",
      }}
    &gt;</span>
      {/*    Display the RouteName here */}
      <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.title}</span>&gt;</span>{route.name}<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
  )
}
</code></pre>
<p>We can do the same thing with the <code>useNavigationState</code> Hook. It gives us access to the navigation state.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> navigationState = useNavigationState(<span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> state)
<span class="hljs-keyword">let</span> index = navigationState.index
<span class="hljs-keyword">let</span> routes = navigationState.routes.length
<span class="hljs-built_in">console</span>.log(index)
<span class="hljs-built_in">console</span>.log(routes)
</code></pre>
<p>React-navigation offers other hooks as well, for example:</p>
<ul>
<li><code>useFocuseEffect</code> : a side effect hook that, when the screens are loaded, returns the focused screen</li>
<li><code>useLinking</code>: handles deepLinking</li>
</ul>
<p>I highly recommend that you <a target="_blank" href="https://reactnavigation.org/docs/use-navigation/">check them out</a>.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>The new react-navigation API definitely moves from static to dynamic. It’s a great direction that will absolutely change the way we handle navigation in React Native. Dynamic routes were a major request by react-navigation users, and this new way will help us create a better user navigation experience.</p>
<h3 id="heading-you-can-find-more-content-about-react-native-herehttpssaidhayanicom">You can find more content about <a target="_blank" href="https://saidhayani.com/">React Native here</a></h3>
<blockquote>
<p>Thanks for reading</p>
</blockquote>
<ul>
<li><a target="_blank" href="https://twitter.com/SaidHYN">Twitter</a></li>
<li><a target="_blank" href="https://github.com/hayanisaid">GitHub</a></li>
<li><a target="_blank" href="https://webege.us16.list-manage.com/subscribe?u=311846a57d1e1a666287ad128&amp;id=2b386b2ebb">Join the mail-list</a></li>
</ul>
<blockquote>
<p>Looking for a React Native developer for your project? <strong><a target="_blank" href="mailto:info.said.dev@gmail.com">Hit me up</a></strong>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
