<?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: arshkkk</title>
    <description>The latest articles on DEV Community by arshkkk (@arshkkk).</description>
    <link>https://dev.to/arshkkk</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%2F736949%2F9c398209-2f14-48c7-9e39-3a87f00b9a77.png</url>
      <title>DEV Community: arshkkk</title>
      <link>https://dev.to/arshkkk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arshkkk"/>
    <language>en</language>
    <item>
      <title>How to Use NextJS Loader for Page Transitions</title>
      <dc:creator>arshkkk</dc:creator>
      <pubDate>Wed, 09 Mar 2022 09:29:01 +0000</pubDate>
      <link>https://dev.to/arshkkk/how-to-use-nextjs-loader-for-page-transitions-1cpd</link>
      <guid>https://dev.to/arshkkk/how-to-use-nextjs-loader-for-page-transitions-1cpd</guid>
      <description>&lt;p&gt;&lt;strong&gt;Context&lt;/strong&gt;&lt;br&gt;
NextJs does Automating Code-Splitting on the basis of &lt;a href="https://nextjs.org/docs/basic-features/pages" rel="noopener noreferrer"&gt;pages&lt;/a&gt;, so each time there is a route change, nextjs dynamically loads the necessary modules.&lt;/p&gt;

&lt;p&gt;If you're using Nextjs &lt;a href="https://nextjs.org/docs/api-reference/next/link" rel="noopener noreferrer"&gt;Link&lt;/a&gt; Component, it prefetches the necessary modules related to page Link has href of, whenever it comes in viewport and whenever user hovers over it by default to make user experience smooth.&lt;/p&gt;

&lt;p&gt;But in some scenarios adding a loader for page transitions when modules are heavy or they are fetched yet boosts the user experience a lot.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's create a Loader Svg First, I'm using a svg loader from &lt;a href="https://codepen.io/nikhil8krishnan/pen/rVoXJa" rel="noopener noreferrer"&gt;here&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const SvgLoader = () =&amp;gt; (
  &amp;lt;div style={{width: '100px'}}&amp;gt;
    &amp;lt;svg
      version="1.1"
      id="L4"
      xmlns="http://www.w3.org/2000/svg"
      x="0px"
      y="0px"
      viewBox="0 0 100 100"
      enable-background="new 0 0 0 0"
    &amp;gt;
      &amp;lt;circle fill="#fff" stroke="none" cx="6" cy="50" r="6"&amp;gt;
        &amp;lt;animate attributeName="opacity" dur="1s" values="0;1;0" repeatCount="indefinite" begin="0.1" /&amp;gt;
      &amp;lt;/circle&amp;gt;
      &amp;lt;circle fill="#fff" stroke="none" cx="26" cy="50" r="6"&amp;gt;
        &amp;lt;animate attributeName="opacity" dur="1s" values="0;1;0" repeatCount="indefinite" begin="0.2" /&amp;gt;
      &amp;lt;/circle&amp;gt;
      &amp;lt;circle fill="#fff" stroke="none" cx="46" cy="50" r="6"&amp;gt;
        &amp;lt;animate attributeName="opacity" dur="1s" values="0;1;0" repeatCount="indefinite" begin="0.3" /&amp;gt;
      &amp;lt;/circle&amp;gt;
    &amp;lt;/svg&amp;gt;
  &amp;lt;/div&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now Create  Loader Component that has the logic for when to show the loader.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import { SvgLoader } from "./SvgLoader";

export const Loader = () =&amp;gt; {
  const router = useRouter();
  const [isLoaderVisible, setIsLoaderVisible] = useState(false);

  React.useEffect(() =&amp;gt; {
    const handleRouteChange = (url, { shallow }) =&amp;gt; {
      setIsLoaderVisible(true);
    };

    const handleRouteComplete = (url, { shallow }) =&amp;gt; {
      setIsLoaderVisible(false);
    };

// here we subscribe to router change start and complete events
    router.events.on("routeChangeStart", handleRouteChange);
    router.events.on("routeChangeComplete", handleRouteComplete);

// unsubscribing to router events when component unmounts to prevent memeory leaks
    return () =&amp;gt; {
      router.events.off("routeChangeStart", handleRouteChange);
      router.events.off("routeChangeComplete", handleRouteComplete);
    };
  }, []);

  if (isLoaderVisible) {
    return (
      &amp;lt;div
        style={{
          position: "fixed",
          inset: 0,
          display: "flex",
          justifyContent: "center",
          alignItems: "center",
        }}
      &amp;gt;
        &amp;lt;SvgLoader /&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  } else return null;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now we can use the above loader in any of the Higher Order compoents or even in _app.js
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {Loader} from '../components/Loader'

function MyApp({Component, pageProps}: any) {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Loader /&amp;gt;
      &amp;lt;Component {...pageProps} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
Whenever there will be a page change, below loader will show up, incase modules are not imported.&lt;br&gt;
Gist: &lt;a href="https://gist.github.com/arshkkk/b3555c65441ff170d942c4e5309360a2" rel="noopener noreferrer"&gt;https://gist.github.com/arshkkk/b3555c65441ff170d942c4e5309360a2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffafe1ojhyzexrjyjb85j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffafe1ojhyzexrjyjb85j.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>loader</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
