<?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>DEV Community: A.S.M. Habibullah Sadique</title>
    <description>The latest articles on DEV Community by A.S.M. Habibullah Sadique (@mhsadique).</description>
    <link>https://dev.to/mhsadique</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%2F812088%2F071d2813-3d62-440f-8b99-eb1432a2729c.jpg</url>
      <title>DEV Community: A.S.M. Habibullah Sadique</title>
      <link>https://dev.to/mhsadique</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mhsadique"/>
    <language>en</language>
    <item>
      <title>All about NextJS routing</title>
      <dc:creator>A.S.M. Habibullah Sadique</dc:creator>
      <pubDate>Mon, 25 Apr 2022 02:34:17 +0000</pubDate>
      <link>https://dev.to/mhsadique/all-about-nextjs-routing-16e3</link>
      <guid>https://dev.to/mhsadique/all-about-nextjs-routing-16e3</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It has a file-system based routing mechanism&lt;/li&gt;
&lt;li&gt;When a file is added to the pages folder in a project, it automatically becomes available as a route&lt;/li&gt;
&lt;li&gt;By mixing and matching file names with a nested folder structure, it is possible to pretty much define the most common routing patterns&lt;/li&gt;
&lt;li&gt;Routing variations are: 

&lt;ul&gt;
&lt;li&gt;Route with pages&lt;/li&gt;
&lt;li&gt;Nested routes&lt;/li&gt;
&lt;li&gt;Dynamic routes&lt;/li&gt;
&lt;li&gt;Catch-all routes&lt;/li&gt;
&lt;li&gt;Navigate from the UI&lt;/li&gt;
&lt;li&gt;Programmatically navigate between pages&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Routing with pages
&lt;/h3&gt;

&lt;p&gt;It has a file-system-based routing mechanism.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Scenario 01: index.js file within the pages folder will map to the root (&lt;a href="https://localhost:3000"&gt;https://localhost:3000&lt;/a&gt;) of your domain. &lt;/li&gt;
&lt;li&gt;Scenario 02: Pages are associated with routes based on their names. In the pages folder, &lt;strong&gt;about.js&lt;/strong&gt; maps to &lt;a href="https://localhost:3000/about"&gt;https://localhost:3000/about&lt;/a&gt; and &lt;strong&gt;profile.js&lt;/strong&gt; maps to &lt;a href="https://localhost:3000/profile"&gt;https://localhost:3000/profile&lt;/a&gt; in the address bar.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Nested routes
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Scenario 01: &lt;strong&gt;pages-&amp;gt;blog-&amp;gt;index.js&lt;/strong&gt; maps to &lt;a href="https://localhost:3000/blog"&gt;https://localhost:3000/blog&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Scenario 02: &lt;strong&gt;pages-&amp;gt;blog-&amp;gt;first.js&lt;/strong&gt; maps to &lt;a href="https://localhost:3000/blog/first"&gt;https://localhost:3000/blog/first&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Scenario 03: &lt;strong&gt;pages-&amp;gt;blog-&amp;gt;second.js&lt;/strong&gt; maps to &lt;a href="https://localhost:3000/blog/second"&gt;https://localhost:3000/blog/second&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Dynamic routes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To implement dynamic routes, you need to name a component by wrapping it with square brackets like &lt;strong&gt;[componentName].js&lt;/strong&gt; and &lt;strong&gt;componentName&lt;/strong&gt; will correspond to the dynamic value in the &lt;strong&gt;url&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pages-&amp;gt;product-&amp;gt;[productId].js&lt;/strong&gt; maps to &lt;a href="https://localhost:3000/product/1"&gt;https://localhost:3000/product/1&lt;/a&gt;, &lt;a href="https://localhost:3000/product/2"&gt;https://localhost:3000/product/2&lt;/a&gt; or even &lt;a href="https://localhost:3000/product/100"&gt;https://localhost:3000/product/100&lt;/a&gt;. The number/string after the &lt;strong&gt;/product/&lt;/strong&gt; in the &lt;strong&gt;url&lt;/strong&gt; will dynamically correspond and map to &lt;strong&gt;[productId].js&lt;/strong&gt; component and the number/string after &lt;strong&gt;/product/&lt;/strong&gt; can be found inside &lt;strong&gt;[productId].js&lt;/strong&gt; component using the &lt;strong&gt;useRouter&lt;/strong&gt; hook like this -
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRouter&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;productId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="c1"&gt;// productId is the dynamic component’s name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;But if a number/string after &lt;strong&gt;/product/&lt;/strong&gt; in the &lt;strong&gt;url&lt;/strong&gt; is matched with the name of a component already existing inside the &lt;strong&gt;product&lt;/strong&gt; page, that component with the matched name will get priority over &lt;strong&gt;[productId].js&lt;/strong&gt; and be rendered rather than &lt;strong&gt;[productId].js&lt;/strong&gt; being rendered.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Nested dynamic routes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To implement nested dynamic routes like &lt;a href="https://localhost:3000/product/7/review/2"&gt;https://localhost:3000/product/7/review/2&lt;/a&gt;, you need a folder structure like &lt;strong&gt;pages/product/[productId]/review/[reviewId].js&lt;/strong&gt;. Note that this time &lt;strong&gt;[productId]&lt;/strong&gt; is a folder, not a &lt;strong&gt;.js&lt;/strong&gt; file. 

&lt;ul&gt;
&lt;li&gt;So, &lt;strong&gt;pages/product/[productId]/review/[reviewId].js&lt;/strong&gt; maps to &lt;a href="https://localhost:3000/product/7/review/2"&gt;https://localhost:3000/product/7/review/2&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;In this mechanism, to achieve the functionality of &lt;a href="https://localhost:3000/product/1"&gt;&lt;/a&gt;&lt;a href="https://localhost:3000/product/1"&gt;https://localhost:3000/product/1&lt;/a&gt;, (only dynamic, not nested dynamic) you need a folder structure like &lt;strong&gt;pages/product/[productId]/index.js&lt;/strong&gt;. You will find &lt;strong&gt;productId&lt;/strong&gt; in the &lt;strong&gt;url&lt;/strong&gt; dynamically in the &lt;strong&gt;index.js&lt;/strong&gt; file.&lt;/li&gt;
&lt;li&gt;Thus you can implement dynamic nested routes by having dynamic segments in the folder name as well as file name.&lt;/li&gt;
&lt;li&gt;Speaking of dynamic multiple path segments, Next JS does offer another feature to handle the same. To know more about the feature, read the next topic. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Catch all routes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This feature applies to scenario where the &lt;strong&gt;url&lt;/strong&gt; contains multiple route segments like &lt;a href="https://localhost:3000/docs/feature1/concept1"&gt;https://localhost:3000/docs/feature1/concept1&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The folder structure for &lt;a href="https://localhost:3000/docs/feature1/concept1"&gt;https://localhost:3000/docs/feature1/concept1&lt;/a&gt; should be &lt;strong&gt;pages/docs/[...params].js&lt;/strong&gt; and inside &lt;strong&gt;[...params].js&lt;/strong&gt; you can use &lt;strong&gt;useRouter&lt;/strong&gt; to get both &lt;strong&gt;feature/&lt;/strong&gt; and &lt;strong&gt;concept/&lt;/strong&gt; or even more to it like below -
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRouter&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;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="c1"&gt;// assigned an empty array because initially it is undefined &lt;/span&gt;
    &lt;span class="c1"&gt;// and may give an error in production&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ "features1", "concept1" ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After getting your dynamic route segments, You can then conditionally render your components or JSX elements like this -
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Doc&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="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRouter&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;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="c1"&gt;// assigned an empty array because initially it is undefined &lt;/span&gt;
    &lt;span class="c1"&gt;// and may give an error in production&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ "features1", "concept1" ]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Viewing&lt;/span&gt; &lt;span class="nx"&gt;docs&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;feature&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;concept&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;)}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Viewing&lt;/span&gt; &lt;span class="nx"&gt;docs&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;feature&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Docs&lt;/span&gt; &lt;span class="nx"&gt;home&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;But the folder structure &lt;strong&gt;pages/docs/[...params].js&lt;/strong&gt; will give a &lt;strong&gt;404&lt;/strong&gt; while trying to access &lt;a href="https://localhost:3000/docs"&gt;https://localhost:3000/docs&lt;/a&gt;. To have a quick fix, you can put another pair of square brackets around &lt;strong&gt;[params].js&lt;/strong&gt;, so the new folder structure is &lt;strong&gt;pages/docs/[[...params]].js&lt;/strong&gt; and &lt;a href="https://localhost:3000/docs"&gt;https://localhost:3000/docs&lt;/a&gt; does not give a &lt;strong&gt;404&lt;/strong&gt; and works as expected.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Link component navigation
&lt;/h3&gt;

&lt;p&gt;You can navigate in the UI using &lt;strong&gt;Link&lt;/strong&gt; component offered by Next.js like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;product/2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;replace&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Go&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Navigating programmatically
&lt;/h3&gt;

&lt;p&gt;You can navigate programmatically using &lt;strong&gt;useRouter&lt;/strong&gt;.  All other routing variations are valid in this mechanism.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&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="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRouter&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;handleOrder&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Placing the order...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/product&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOrder&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Place&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;h3&gt;
  
  
  Custom 404 page
&lt;/h3&gt;

&lt;p&gt;To provide your custom &lt;strong&gt;404&lt;/strong&gt; page, create a &lt;strong&gt;404.js&lt;/strong&gt; file in the pages folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;So, that is all about routing in NextJS. Hope you liked it. Thanks for reading.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Next.js features I'm excited about</title>
      <dc:creator>A.S.M. Habibullah Sadique</dc:creator>
      <pubDate>Fri, 15 Apr 2022 15:43:53 +0000</pubDate>
      <link>https://dev.to/mhsadique/nextjs-features-im-excited-about-5elg</link>
      <guid>https://dev.to/mhsadique/nextjs-features-im-excited-about-5elg</guid>
      <description>&lt;h2&gt;
  
  
  Stable Incremental Static Regeneration
&lt;/h2&gt;

&lt;p&gt;Simply speaking, this feature allows you to generate static content dynamically. Let's see a quick example:&lt;/p&gt;

&lt;p&gt;Say you have a blog website with a lot of articles. When somebody visits /news/&lt;a href="https://dev.towhere%20%5Blink%5D%20is%20anything"&gt;link&lt;/a&gt;, you want to serve them the static page as fast as you can.&lt;/p&gt;

&lt;p&gt;But it is possible that you don't want to create all static pages at build time because it would take you a lot of time. In a few cases, this isn't possible at all at build time.&lt;/p&gt;

&lt;p&gt;Also, sometimes your content might change, like a quick blog edit - so you don't really want a completely static page forever either. So what's the solution?&lt;/p&gt;

&lt;p&gt;Using Next.js 9.5+, now you can generate static pages dynamically to the dynamic path and refresh them.&lt;/p&gt;

&lt;p&gt;This means that once Next fetches that particular URL, it'll save it as a static page and serve it statically whenever somebody visits the path. At the same time, it'll be open to accepting new paths dynamically.&lt;/p&gt;

&lt;p&gt;Not only can you do this, with a revalidate parameter, you can also actually specify that Next.js should update your static pages once every X seconds in the background if there's any change!&lt;/p&gt;

&lt;h2&gt;
  
  
  getServerSideProps
&lt;/h2&gt;

&lt;p&gt;getServerSideProps, as the name suggests, allows you to inject props in your Next.js page from the server itself. And getStaticProps allows Next.js to still create static outputs at build time.&lt;/p&gt;

&lt;p&gt;getStaticProps combined with incremental static regeneration is a killer feature in my opinion. You get the many benefits of a dynamic backend without having a dynamic backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Persistent Caching for Page Bundles
&lt;/h2&gt;

&lt;p&gt;Next.js also supports persistent caches for pages that are not changed. Before, when you shipped a new Next.js app, Next.js would throw out the whole app and the user had to download all the CSS/JS again, even if those bundles were unchanged.&lt;/p&gt;

&lt;p&gt;In the latest version of Next.js released last week, Vercel introduced persistent caching which is again an absolutely great thing to have performance-wise.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JWT</title>
      <dc:creator>A.S.M. Habibullah Sadique</dc:creator>
      <pubDate>Wed, 09 Feb 2022 15:58:51 +0000</pubDate>
      <link>https://dev.to/mhsadique/jwt-1od8</link>
      <guid>https://dev.to/mhsadique/jwt-1od8</guid>
      <description>&lt;h2&gt;
  
  
  JWT
&lt;/h2&gt;

&lt;p&gt;JSON Web Token structure: &lt;br&gt;
In its compact form, JSON Web Tokens consist of three parts separated by dots (.), which are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Header&lt;/li&gt;
&lt;li&gt;Payload&lt;/li&gt;
&lt;li&gt;Signature&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, a JWT typically looks like the following: &lt;code&gt;xxxxxx.yyyyyy.zzzzzz&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Header: *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The header typically consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA. &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    “alg”: “HS256, 
    “typ”: “JWT”
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then this JSON IS Base64Url encoded to form the first part of the JWT. &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Payload: *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Registered claims:&lt;/strong&gt; These are a set of predefined claims which are not mandatory but recommended, to provide a set of useful, interoperable claims. Some of them are: iss (issuer), exp (expiration time), sub (subject), aud (audience), and others.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Public claims:&lt;/strong&gt; These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Private claims:&lt;/strong&gt; These are the custom claims created to share information between parties that agree on using them and are neither registered or public claims.&lt;/p&gt;

&lt;p&gt;An example payload could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    “sub”: “131242134”,
    “name”: “Sadique Habibullah”,
    “admin”: true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The payload is then Base64Url encoded to form the second part of the JSON Web Token. &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Signature: *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.&lt;/p&gt;

&lt;p&gt;For example if you want to use the &lt;code&gt;HMAC SHA256&lt;/code&gt; algorithm, the signature will be created in the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The signature is used to verify the message wasn't changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Putting it altogether: *&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;The output is three Base64-URL strings separated by dots that can be easily passed in HTML and HTTP environments, while being more compact when compared to XML-based standards such as SAML.&lt;/p&gt;

&lt;p&gt;Finally it becomes something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTY0NDQxNzA1NCwiZXhwIjoxNjQ0NDIwNjU0fQ.GiJsRD3463h80YlAYG92UupWcot7jWRHNhnNpnBV2bQ 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>React.useCallback()</title>
      <dc:creator>A.S.M. Habibullah Sadique</dc:creator>
      <pubDate>Wed, 09 Feb 2022 13:32:41 +0000</pubDate>
      <link>https://dev.to/mhsadique/reactusecallback-134g</link>
      <guid>https://dev.to/mhsadique/reactusecallback-134g</guid>
      <description>&lt;p&gt;Before diving into &lt;code&gt;useCallback()&lt;/code&gt; usage, let's distinguish the problem &lt;code&gt;useCallback()&lt;/code&gt; solves — the functions equality check. &lt;/p&gt;

&lt;p&gt;Functions in JavaScript are first-class citizens, meaning that a function is a regular object. The function object can be returned by other functions, be compared, etc.: anything you can do with an object.&lt;/p&gt;

&lt;p&gt;Let's write a function &lt;code&gt;factory()&lt;/code&gt; that returns functions that sum numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function factory() {
  return (a, b) =&amp;gt; a + b;
}
const sum1 = factory();
const sum2 = factory();
sum1(1, 2); // =&amp;gt; 3
sum2(1, 2); // =&amp;gt; 3
sum1 === sum2; // =&amp;gt; false
sum1 === sum1; // =&amp;gt; true`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sum1&lt;/code&gt; and &lt;code&gt;sum2&lt;/code&gt; are functions that sum two numbers. They've been created by the &lt;code&gt;factory()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The functions &lt;code&gt;sum1&lt;/code&gt; and &lt;code&gt;sum2&lt;/code&gt; share the same code source but they are different function objects. Comparing them &lt;code&gt;sum1 === sum2&lt;/code&gt;evaluates to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The purpose of &lt;code&gt;useCallback()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Different function objects sharing the same code are often created inside React components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyComponent() {
  // handleClick is re-created on each render
  const handleClick = () =&amp;gt; {
    console.log('Clicked!');
  };
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;handleClick&lt;/code&gt; is a different function object on every rendering of &lt;code&gt;MyComponent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because inline functions are cheap, the re-creation of functions on each rendering is not a problem. A few inline functions per component are acceptable.&lt;/p&gt;

&lt;p&gt;But in some cases you need to maintain a single function instance between renderings:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A functional component wrapped inside &lt;code&gt;React.memo()&lt;/code&gt; accepts a function object prop&lt;/li&gt;
&lt;li&gt;When the function object is a dependency to other hooks, e.g. &lt;code&gt;useEffect(..., [callback])&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;When the function has some internal state, e.g. when the function is debounced or throttled.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's when &lt;code&gt;useCallback(callbackFun, deps)&lt;/code&gt; is helpful: given the same dependency values &lt;code&gt;deps&lt;/code&gt;, the hook returns the same function instance between renderings (aka memoization):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useCallback } from 'react';
function MyComponent() {
  // handleClick is the same function object
  const handleClick = useCallback(() =&amp;gt; {
    console.log('Clicked!');
  }, []);
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;handleClick&lt;/code&gt; variable has always the same callback function object between renderings of &lt;code&gt;MyComponent&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>performance</category>
      <category>hook</category>
    </item>
  </channel>
</rss>
