<?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: Shailesh Kumar</title>
    <description>The latest articles on DEV Community by Shailesh Kumar (@shailseen).</description>
    <link>https://dev.to/shailseen</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%2F877608%2Fc6403a85-5fae-4605-97a6-39577a417558.jpeg</url>
      <title>DEV Community: Shailesh Kumar</title>
      <link>https://dev.to/shailseen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shailseen"/>
    <language>en</language>
    <item>
      <title>5 cool vs code extensions for web developers</title>
      <dc:creator>Shailesh Kumar</dc:creator>
      <pubDate>Fri, 22 Jul 2022 12:25:02 +0000</pubDate>
      <link>https://dev.to/shailseen/5-cool-vs-code-extensions-for-web-developers-1kfj</link>
      <guid>https://dev.to/shailseen/5-cool-vs-code-extensions-for-web-developers-1kfj</guid>
      <description>&lt;p&gt;Hi all, today is this blog I am going to show 5 vs code extensions which I use that boosts my productivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Inline Fold
&lt;/h2&gt;

&lt;p&gt;If you have been using Tailwind css(which you definitely should) then you might want to hide the styles you have applied once you are done with the styling. Using this extension the classNames collapses to three dots and help us focus more on the code.&lt;/p&gt;

&lt;p&gt;before&lt;br&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%2Flq8wmtfyvwq0fh69r5kb.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%2Flq8wmtfyvwq0fh69r5kb.png" alt="before"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after&lt;br&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%2F1btxlcwqlpbn4zvuvtq7.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%2F1btxlcwqlpbn4zvuvtq7.png" alt="after"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="'https://marketplace.visualstudio.com/items?itemName=moalamri.inline-fold'"&gt;Inline Fold&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Tailwind CSS IntelliSense
&lt;/h2&gt;

&lt;p&gt;The second extension is also on Tailwind CSS 😛 . As it is clear from the name, this extension provides auto-complete suggestions for Tailwind CSS. I can't emphasis how slick it makes writing CSS. See below for yourself.&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%2Fezhriui5tymwomv52ptx.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%2Fezhriui5tymwomv52ptx.png" alt="auto complete in tailwind"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="'https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss'"&gt;Tailwind CSS IntelliSense&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Peacock
&lt;/h2&gt;

&lt;p&gt;Peacock gives different colors to different instances of Vs code. This helps in quick identification of which project we are currently working on&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%2Fraw.githubusercontent.com%2Fjohnpapa%2Fvscode-peacock%2Fmain%2Fresources%2Fhero.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%2Fraw.githubusercontent.com%2Fjohnpapa%2Fvscode-peacock%2Fmain%2Fresources%2Fhero.png" alt="peacock"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="'https://marketplace.visualstudio.com/items?itemName=johnpapa.vscode-peacock'"&gt;Peacock&lt;/a&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  4. Rainbow Brackets
&lt;/h2&gt;

&lt;p&gt;Rainbow Brackets gives different colors of nested brackets in Vs code, by which you can distinguish easily closing and opening of the brackets it can be &lt;strong&gt;parenthesis&lt;/strong&gt;, &lt;strong&gt;square bracket&lt;/strong&gt;, &lt;strong&gt;curly bracket&lt;/strong&gt;  .&lt;br&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%2Fcryze8sxtp2nsqa40xq4.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%2Fcryze8sxtp2nsqa40xq4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="'https://marketplace.visualstudio.com/items?itemName=2gua.rainbow-brackets'"&gt;Rainbow Brackets&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. CodeSnap
&lt;/h2&gt;

&lt;p&gt;When you have writing a blog or make documentation of your projects and want to give your code with cool looking then this extension is best for you. You can easily install this extension on Vs code then three steps you have to follow.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select the code you want to take snap.&lt;/li&gt;
&lt;li&gt;Right click, choose CodeSnap📸.&lt;/li&gt;
&lt;li&gt;After you get an snap of your code, click the image and press the copy keyboard shortcut (defaults are Ctrl+C on Windows and Linux, Cmd+C on OS X)&lt;/li&gt;
&lt;/ul&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%2F3b6o0kpb2qa6gy7mm6vp.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%2F3b6o0kpb2qa6gy7mm6vp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="'https://marketplace.visualstudio.com/items?itemName=adpyke.codesnap'"&gt;CodeSnap📸&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>vscode</category>
    </item>
    <item>
      <title>React Memo, useMemo, useCallback - Optimize your performance</title>
      <dc:creator>Shailesh Kumar</dc:creator>
      <pubDate>Fri, 17 Jun 2022 13:37:37 +0000</pubDate>
      <link>https://dev.to/shailseen/react-memo-usememo-usecallback-optimize-your-performance-216</link>
      <guid>https://dev.to/shailseen/react-memo-usememo-usecallback-optimize-your-performance-216</guid>
      <description>&lt;p&gt;Hello everyone!&lt;br&gt;
In this blog we talk about how to optimize your react project.&lt;/p&gt;

&lt;p&gt;In react there is a hook &lt;strong&gt;React.memo&lt;/strong&gt; for optimizing the performance , and there is a &lt;strong&gt;React.useMemo&lt;/strong&gt; also for optimizing the performance , another one is &lt;strong&gt;React.useCallback&lt;/strong&gt; also for optimizing performance. so you are confused 😑 which one to used ?&lt;br&gt;
let's dive in to the examples to understand you perfectly.&lt;/p&gt;
&lt;h2&gt;
  
  
  Scenario where need to optimize
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/goofy-gwen-6xjnhu"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In the above example there is component named as &lt;strong&gt;Child&lt;/strong&gt; &lt;br&gt;
and there is a counter state in &lt;strong&gt;parent/app.js&lt;/strong&gt;. When you increment the counter the &lt;strong&gt;Child component&lt;/strong&gt; also will be re-rendered (You can see the console after each click on the button)&lt;br&gt;
while there is no state change in the child element ,then component will be re-rendered.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is the solution for stop un-usual re-rendering ?
&lt;/h3&gt;

&lt;p&gt;There is a concept called &lt;strong&gt;Memoization&lt;/strong&gt;. &lt;br&gt;
Let's see the definition of memoization&lt;/p&gt;
&lt;h2&gt;
  
  
  What is memoization ?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Memoization&lt;/strong&gt; is an optimization technique that makes applications more efficient and hence faster🚀. It does this by storing computation results in cache, and retrieving that same information from the cache the next time it's needed instead of computing it again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A memoized function remembers the result of an output for a given set of inputs. It means when you give it set of inputs it remembers those inputs and also output associative with it. Whenever passing a set of input to the memoized function first it check in the &lt;strong&gt;cached memory&lt;/strong&gt; whether the set of inputs is available or not, if they have its give you computed value from the cached memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In React, we can memoized components (where the inputs are props) or functions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's memoize our previous example:&lt;br&gt;
We use &lt;strong&gt;React Memo&lt;/strong&gt; for optimize this component example.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/fervent-oskar-gfur87"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can now check the console only first time render the child component after using React Memo.&lt;/p&gt;

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

&lt;p&gt;MDN definition  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's add an input field to the above example and pass props to child component function(setCounter) in which increase the counter value by calling this function.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/admiring-feather-simjzu"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In above codeSandBox example if we type in the input field then child component get re-rendered(You can see console), while there is no props pass to the Child component which is related to state Input text field. Then why this happening ?🤔&lt;/p&gt;

&lt;p&gt;The child component get re-render because when the parent component render in each time the inline function which is pass in the child component is &lt;strong&gt;redefining the function calls another function which is called setCounter&lt;/strong&gt;.&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="c1"&gt;// passing function to the component&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;updateCounter&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="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;counter&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;counter&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="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we want to pass callback function with memoize, so &lt;strong&gt;React Memo&lt;/strong&gt; working properly. We use &lt;strong&gt;useCallback&lt;/strong&gt; to memoize the callback functions. Let's code and look how can we use useCallback hook.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/crazy-torvalds-oh1xzy"&gt;
&lt;/iframe&gt;
&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateCounterFromChild&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&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;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&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="nx"&gt;counter&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="c1"&gt;// Some Jsx returns&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Child&lt;/span&gt; &lt;span class="nx"&gt;updateCounter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;updateCounterFromChild&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&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;p&gt;In useCallback({},[]) there is a dependency array list and we add dependency &lt;strong&gt;counter&lt;/strong&gt; because the updateCounterFromChild function redefined only when the counter state is changed.&lt;/p&gt;

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

&lt;p&gt;Definition by ⚛️reacts.org:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;useMemo&lt;/strong&gt; will only recompute the memoized value when one of the dependencies has changed. This optimization helps to &lt;strong&gt;avoid expensive calculations&lt;/strong&gt; on every render.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's understand with the above statement they said avoid expensive calculations on every render.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FXN4gJBs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cw70thd4c08sr1ibglhj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FXN4gJBs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cw70thd4c08sr1ibglhj.gif" alt="Image description" width="491" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/boring-minsky-3mofqj"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In above example you can notice that there is very expensive calculation will be done as below you can see.&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="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&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="s2"&gt;Expensive calucation to be started...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tempNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="nx"&gt;_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;tempNumber&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="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In every click there is an delay for render the state of counter, because every click IIFE is called and doing expensive calculations. we don't want to re-rendered IIFE because every render we get same value from the previous render and this reason memoization comes.&lt;/p&gt;

&lt;p&gt;We do two types of memoization in single example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Don't calculate expensive calculation in each render.&lt;/li&gt;
&lt;li&gt;I want to we update only &lt;strong&gt;Second counter&lt;/strong&gt; in above example.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  useMemo vs useEffect
&lt;/h3&gt;

&lt;p&gt;If you think we can do second part of the above statement with the help of &lt;strong&gt;useEffect&lt;/strong&gt; then you can't do with this because, useEffect work for whole component of the app but useMemo can work for some part of the component. let's see with the code how useMemo work for some part of the HTML code.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/musing-williamson-q9vzt3"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you can see the above codeSandBox output the re-render after every click becomes smooth and there is no console of the IIFE.&lt;br&gt;
First counter not get affected by the Increment click. If you want to update also the state of first counter then you pass dependencies array &lt;strong&gt;counter&lt;/strong&gt; to the useMemo. &lt;/p&gt;

&lt;p&gt;Thanks for reading this blog with the patience 😊, and i suppose to be that you will get learn something from this blog.&lt;br&gt;
Give your love💖 and feedback💬 to this blog.    &lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Why Intersection Observer ?</title>
      <dc:creator>Shailesh Kumar</dc:creator>
      <pubDate>Wed, 15 Jun 2022 22:09:14 +0000</pubDate>
      <link>https://dev.to/shailseen/why-intersection-observer--1gnj</link>
      <guid>https://dev.to/shailseen/why-intersection-observer--1gnj</guid>
      <description>&lt;h2&gt;
  
  
  What is Intersection Observer API
&lt;/h2&gt;

&lt;p&gt;By the mdn docs&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Understand
&lt;/h2&gt;

&lt;p&gt;This concept made by two words first is &lt;strong&gt;Intersection&lt;/strong&gt; and another one is &lt;strong&gt;Observer&lt;/strong&gt;. let's take basic view on this topic first.&lt;/p&gt;

&lt;p&gt;By the name you can understand that the intersection means minimum to objects, in this concept the intersection taken with the one is viewport and another one is with the element such as div,img. &lt;/p&gt;

&lt;p&gt;Observer is observe the viewport when the specific target element by you comes in the viewport then its give you an details the item you targeted is comes to your viewport. Obviously target element comes in to the viewport by scroll the page.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can we do by observing the element 🤔 ?
&lt;/h2&gt;

&lt;p&gt;Nowdays the website using this concept for giving animation on the element like: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://7jnpj.csb.app/" rel="noopener noreferrer"&gt;image lazy loading effect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pho2j.csb.app/" rel="noopener noreferrer"&gt;Infinite scrolling on page&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://gqcc8.csb.app/" rel="noopener noreferrer"&gt;Framer motion animated in viewport&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most impressive and bit a complex example of this &lt;a href="https://www.apple.com/in/iphone-13/" rel="noopener noreferrer"&gt;Apple website&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%2Fwbua1unjrqopvf3uzicg.gif" 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%2Fwbua1unjrqopvf3uzicg.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not scroll event ? 😒
&lt;/h2&gt;

&lt;p&gt;Scroll event is an event listener when the target element scroll is keep calling the function when the element comes in the viewport and by this reason your animation get continuously render without any delay and it will not giving you output as your desired.&lt;/p&gt;

&lt;p&gt;let's see this example.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/SitePoint/embed/WxZjQY?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;For handle this you need to use &lt;strong&gt;Throttling&lt;/strong&gt; for delay on your event handler function calling.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/SitePoint/embed/RRLVrL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When you use Intersection Observer you don't need for any throttling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For handle the this type of problem there is an &lt;strong&gt;Intersect Ratio&lt;/strong&gt; by the observer this tells that how much part of the targeted element rendered in to the viewport.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is an &lt;strong&gt;Threshold value&lt;/strong&gt; which provided by the Intersection Observer API. It provide one or more numeric values representing percentages of the target element which are visible on the viewport.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In scroll event you need to calculate some complex calculation for get the value of the height view of the targeted element. In intersection observer you get these all values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Intersect Ratio you can use for observe the targeted element. By the docs &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The degree of intersection between the target element and its root is the intersection ratio. This is a representation of the percentage of the target element which is visible as a value between 0.0 and 1.0.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here 0.0 means 0%, 0.5 means 50%, 1.0 means 100% visible on the viewport.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example for change the page background color on scrolling by Intersection Observer API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/dmlb/embed/abvQzLe?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Start with code
&lt;/h2&gt;

&lt;p&gt;By the reference of MDN Docs&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;let&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#scrollArea&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;rootMargin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the targeted element meet the threshold value specified by the &lt;strong&gt;IntersectionObserver&lt;/strong&gt; the function callback is invoked. &lt;/p&gt;

&lt;p&gt;The callback receives a list of IntersectionObserverEntry objects.&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%2Fasb3muyuz2vjwew8zme5.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%2Fasb3muyuz2vjwew8zme5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example by MDN&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/shailseen/embed/poaYoqR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Thanks for reading my blog 😊&lt;br&gt;
Give your feedback and suggestions.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
