<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rishabh</title>
    <description>The latest articles on Forem by Rishabh (@rishabhrpg).</description>
    <link>https://forem.com/rishabhrpg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F807829%2Fd80a8487-afaa-47a9-89a5-1ad25c611a79.png</url>
      <title>Forem: Rishabh</title>
      <link>https://forem.com/rishabhrpg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rishabhrpg"/>
    <language>en</language>
    <item>
      <title>Effortlessly Highlight Active Links in Your Next.js Application with useActiveRouter Hook</title>
      <dc:creator>Rishabh</dc:creator>
      <pubDate>Sun, 07 May 2023 15:41:39 +0000</pubDate>
      <link>https://forem.com/rishabhrpg/effortlessly-highlight-active-links-in-your-nextjs-application-with-useactiverouter-hook-mfb</link>
      <guid>https://forem.com/rishabhrpg/effortlessly-highlight-active-links-in-your-nextjs-application-with-useactiverouter-hook-mfb</guid>
      <description>&lt;p&gt;Highlighting the active link in a Next.js application with multiple pages and navigation links can be a tedious and error-prone task. However, the useActiveRouter hook from the active-link-nextjs package can simplify this process. By defining a configuration object with the active class name and using the registerRoute function returned by the hook with the Link component, the active link can be easily highlighted.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is useActiveRouter?
&lt;/h2&gt;

&lt;p&gt;useActiveRouter is a nextjs hook that can be imported from &lt;a href="https://www.npmjs.com/package/active-link-nextjs" rel="noopener noreferrer"&gt;active-link-nextjs&lt;/a&gt; package. To install this package you can do&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install active-link-nextjs&lt;/code&gt;&lt;br&gt;
This hook takes a config object as parameter where you can define the name of active class to be auto applied to active link.&lt;br&gt;
it returns an object containing href and className properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useActiveRouter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;active-link-nextjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next/link&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;activeClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;registerRoute&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useActiveRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="nf"&gt;registerRoute&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="nf"&gt;registerRoute&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;About&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="nf"&gt;registerRoute&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Blog&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we first import the useActiveRouter hook from the &lt;code&gt;active-link-nextjs&lt;/code&gt; package and the Link component from the &lt;code&gt;next/link&lt;/code&gt; package. We then define a configuration object with an &lt;code&gt;activeClass&lt;/code&gt; property set to &lt;code&gt;'active'&lt;/code&gt;. This is the class name that will be added to the active link's a tag.&lt;/p&gt;

&lt;p&gt;We then call useActiveRouter with the configuration object, which returns the &lt;code&gt;registerRoute&lt;/code&gt; function. We can then use this function to generate the &lt;strong&gt;&lt;code&gt;href&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;className&lt;/code&gt;&lt;/strong&gt; properties for each navigation link.&lt;/p&gt;

&lt;p&gt;Instead of using an a tag, we wrap the Link component around the result of the registerRoute function. This ensures that the link is pre-fetched by Next.js, which can improve the app's performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use useActiveRouter and Link?
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;useActiveRouter&lt;/code&gt; and Link can simplify the process of adding an active class to the active link in a Next.js app. Instead of manually checking the current route and adding the class name, we can use the registerRoute function to handle this for us. This can save us time and reduce the chance of errors, especially if our app has many pages and nested routes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you’re building a Next.js app with multiple pages and navigation links, useActiveRouter and Link can help simplify the process of highlighting the active link. By using the registerRoute function and Link component&lt;/p&gt;

&lt;p&gt;Contributions and suggestions are welcome &lt;a href="https://github.com/rishabhrpg/active-link-nextjs" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>nextrouter</category>
      <category>react</category>
    </item>
  </channel>
</rss>
