<?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: Muhammad Naufal</title>
    <description>The latest articles on DEV Community by Muhammad Naufal (@naufaal98).</description>
    <link>https://dev.to/naufaal98</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%2F410403%2Fc6097e66-23d1-47ca-ac4f-07816970e9e3.jpeg</url>
      <title>DEV Community: Muhammad Naufal</title>
      <link>https://dev.to/naufaal98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naufaal98"/>
    <language>en</language>
    <item>
      <title>Lazy Loading Images in React for Better Performance</title>
      <dc:creator>Muhammad Naufal</dc:creator>
      <pubDate>Tue, 16 Jun 2020 16:49:16 +0000</pubDate>
      <link>https://dev.to/naufaal98/lazy-loading-images-in-react-for-better-performance-156i</link>
      <guid>https://dev.to/naufaal98/lazy-loading-images-in-react-for-better-performance-156i</guid>
      <description>&lt;p&gt;In some cases, we need to load a list that contains a lot of images on a single page. Most of the images not even visible on the screen when it is loaded. We need to scroll down to see them. By default, when we open the page, the browser would load and request all the images on the page regardless if it’s visible on the viewport or not. That’s perfectly fine in some conditions, but when we have a lot of images that are not visible on the viewport and load all of them at the same time, that may lead to performance issues.&lt;/p&gt;

&lt;p&gt;In that condition, it’s better to only load the images that appear on the viewport. It improves the performance cause the browser will postpone loading the images below the viewport until the user scrolls the page and reach the images.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s Start
&lt;/h2&gt;

&lt;p&gt;There are several ways to do lazy loading including a native way, but when I write this post, native lazy loading is only supported in Chromium-based browsers and Firefox. So for wider browsers support, we’re going to do lazy loading using react-lazyload, and styled-components for styling. You can start by installing these packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save react-lazyload styled-components

// or if you prefer yarn:

yarn add react-lazyload styled-components
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Create LazyImage component
&lt;/h2&gt;

&lt;p&gt;We’ll use this component when we want to lazy-load images.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The LazyImage component contains &lt;code&gt;ImageWrapper&lt;/code&gt;, &lt;code&gt;Placeholder&lt;/code&gt;, &lt;code&gt;LazyLoad&lt;/code&gt;, and &lt;code&gt;StyledImage&lt;/code&gt;. Anything inside LazyLoad would not load until it appears on the viewport, that’s why we put &lt;code&gt;StyledImage&lt;/code&gt; inside it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Placeholder&lt;/code&gt; is just an empty &lt;code&gt;div&lt;/code&gt; with animation to indicate the image is still loading. When the image finally loaded then we call &lt;code&gt;removePlaceholder&lt;/code&gt; to remove &lt;code&gt;Placeholder&lt;/code&gt; from the DOM. I use &lt;code&gt;refs&lt;/code&gt; to do that instead of updating the state to prevent unnecessary re-rendering. If you’re unfamiliar with refs in React, you can learn more about it &lt;a href="https://reactjs.org/docs/refs-and-the-dom.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can create shimmer or put spinner inside the &lt;code&gt;Placeholder&lt;/code&gt;, but I just made it simple here with animated background. Set the &lt;code&gt;Placeholder&lt;/code&gt; size the same as the image size so the transition will be smoother. In the example above, the size of &lt;code&gt;Placeholder&lt;/code&gt; and &lt;code&gt;StyledImage&lt;/code&gt; will follow the &lt;code&gt;ImageWrapper&lt;/code&gt; size.&lt;/p&gt;

&lt;p&gt;You might wanna use react-lazyload placeholder prop to put Placeholder like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;LazyLoad placeholder={&amp;lt;Placeholder /&amp;gt;}&amp;gt;
  ...
&amp;lt;/LazyLoad&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;But when I tried that, the Placeholder would instantly disappear when it reaches the viewport as I scroll the page even though the image is still not fully loaded, hence I put it outside and manage it with onLoad and onError events.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let’s Try It
&lt;/h2&gt;

&lt;p&gt;In App.js, we’ll use dummy images from Picsum Photos to demonstrate and see how the LazyImage works.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;It’s not recommended and considered as &lt;a href="https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318"&gt;anti-pattern&lt;/a&gt; to use indexes for keys. I did that in this example just to make it simple and straightforward to focus on lazy loading.&lt;/p&gt;

&lt;p&gt;When we run the App, it would look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NPApJj9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pxk19e3it5awuxgtiwe3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NPApJj9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pxk19e3it5awuxgtiwe3.gif" alt="The Result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the gif above, images not loaded until it appears on the viewport, which improves the performance and reduces bandwidth consumption. This method also works fine in Server Side Rendering.&lt;/p&gt;

&lt;p&gt;You can still improve it by adding different rendered elements/style when an error happens or create fade-in animation when the image appears, I leave that for you to try.&lt;/p&gt;

&lt;p&gt;I put the code on Codesandbox, you can try and play with it &lt;a href="https://codesandbox.io/s/lazyloading-image-wfei7?file=/src/App.js"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope this post helps you! Thanks for reading!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
