<?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: Aviskar KC</title>
    <description>The latest articles on DEV Community by Aviskar KC (@aviskarkc10).</description>
    <link>https://dev.to/aviskarkc10</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%2F247121%2Fbe93a584-a149-4b79-b687-a1abd8f7ca35.jpeg</url>
      <title>DEV Community: Aviskar KC</title>
      <link>https://dev.to/aviskarkc10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aviskarkc10"/>
    <language>en</language>
    <item>
      <title>JSX gotcha when conditionally rendering using `&amp;&amp;`</title>
      <dc:creator>Aviskar KC</dc:creator>
      <pubDate>Thu, 16 Apr 2020 09:09:29 +0000</pubDate>
      <link>https://dev.to/aviskarkc10/jsx-gotcha-when-conditionally-rendering-using-179j</link>
      <guid>https://dev.to/aviskarkc10/jsx-gotcha-when-conditionally-rendering-using-179j</guid>
      <description>&lt;p&gt;There are tons of ways to conditionally render your React components, with one of them being the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator. For eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// this will only display MyComponent if displayData is a truthy value
{displayData &amp;amp;&amp;amp; &amp;lt;MyComponent data={this.state.data} /&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But what if &lt;code&gt;data&lt;/code&gt; is an array, and you want to make sure &lt;code&gt;data&lt;/code&gt; isn't empty when rendering &lt;code&gt;MyComponent&lt;/code&gt;. A common pattern is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{this.state.data.length &amp;amp;&amp;amp; &amp;lt;MyComponent data={this.state.data} /&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This works fine for the truthy values of &lt;code&gt;data&lt;/code&gt; like when &lt;code&gt;data.length&lt;/code&gt; is greater than &lt;code&gt;0&lt;/code&gt;, but if &lt;code&gt;data.length&lt;/code&gt; is equal to &lt;code&gt;0&lt;/code&gt;, this creates problems. Instead of rendering nothing like we intend to do here, the value &lt;code&gt;0&lt;/code&gt; is rendered. This is because:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// returns MyComponent and it is rendered by React.
{true &amp;amp;&amp;amp; &amp;lt;MyComponent /&amp;gt;}

// returns false and nothing is rendered by React.
{false &amp;amp;&amp;amp; &amp;lt;MyComponent /&amp;gt;}

// data.length returns 3, which is a truthy value, so MyComponent is rendered.
data = [1, 2, 3];
{data.length &amp;amp;&amp;amp; &amp;lt;MyComponent /&amp;gt;

// data.length returns 0, which is falsy value and 0 is rendered.
// Which is a problem as want nothing to be rendered.
data = [];
{data.length &amp;amp;&amp;amp; &amp;lt;MyComponent /&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the last case, &lt;code&gt;0&lt;/code&gt; is rendered because unlike &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt; is an actual valid. You can fix this issue by using a ternary operator whenever you want to conditionally render components by checking for the length of an array or by converting the condition to an expression that returns either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{data.length ? &amp;lt;MyComponent /&amp;gt; : null}

// or

{data.length &amp;gt; 0 &amp;amp;&amp;amp; &amp;lt;MyComponent /&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note this happens with other falsey values that are valid JSX syntax like &lt;code&gt;NaN&lt;/code&gt; and &lt;code&gt;-0&lt;/code&gt; too.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using AbortController out in the wild</title>
      <dc:creator>Aviskar KC</dc:creator>
      <pubDate>Thu, 09 Apr 2020 16:46:15 +0000</pubDate>
      <link>https://dev.to/aviskarkc10/using-abortcontroller-out-in-the-wild-12ah</link>
      <guid>https://dev.to/aviskarkc10/using-abortcontroller-out-in-the-wild-12ah</guid>
      <description>&lt;p&gt;I recently had to cancel requests made by &lt;code&gt;fetch&lt;/code&gt; in one the projects I was working on and had a chance to use &lt;code&gt;AbortController&lt;/code&gt;. Now there are some really good resources to learn about &lt;code&gt;AbortController&lt;/code&gt; like &lt;a href="https://developers.google.com/web/updates/2017/09/abortable-fetch" rel="noopener noreferrer"&gt;this one&lt;/a&gt; by Jake Archibald, but very few showcase using it in a real life scenario. Usually the examples in these tutorials will have a button to make an api call and a second one to cancel it. Although that's a good example to get started with, but I can't think of a case where I might ever build some thing like that in a "real" project. So here's an example where you might actually use &lt;code&gt;AbortController&lt;/code&gt; in a real life scenario.&lt;/p&gt;

&lt;p&gt;Imagine a search bar, where you need to make the api call to fetch data as you type. Something like this:&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%2Fi%2F0uf5pk15sme53a9er1ml.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%2Fi%2F0uf5pk15sme53a9er1ml.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, you will definitely come across a situation where a promise resolves faster than a previous one and you will be left displaying stale data to the user. You can definitely use ol' reliable &lt;a href="https://www.geeksforgeeks.org/debouncing-in-javascript/" rel="noopener noreferrer"&gt;debounce&lt;/a&gt; for this, but that still doesn't solve your issue all the time.&lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;AbortController&lt;/code&gt; comes to your rescue!!! If a new api call is made while the previous one hasn't resolved, you can cancel the previous one using &lt;code&gt;AbortController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you wanna jump into the code straight away here's a &lt;a href="https://codesandbox.io/s/abortcontroller-example-ew3o1" rel="noopener noreferrer"&gt;demo&lt;/a&gt;, but if you wanna know more what's going on, you can follow the blog furthermore:&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;index.html&lt;/code&gt; file, we have our input field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;input
   class="search-field"
   type="text"
   id="search"
   placeholder="Search for a joke"
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On every &lt;code&gt;keyup&lt;/code&gt; event, this input field fires a call to fetch data from our api:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// variable to track whether or not we are fetching data
let isLoading = false;

// event listener for our input field 
searchEl.addEventListener("keyup", e =&amp;gt; {
  fetchQuote(e.target.value);
});

// function to call api and fetch data based on our search query
async function fetchQuote(search) {
  try {
    isLoading = true;

    const response = await fetch(
      `https://api.chucknorris.io/jokes/search?query=${search}`,
      { signal }
    );

    const data = await response.json();

    const jokes = data.result.slice(0, 5);

    isLoading = false;
    renderJokes(jokes);
  } catch (err) {
    isLoading = false;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we have a &lt;code&gt;isLoading&lt;/code&gt; variable to tell us whether or not we have a pending promise.&lt;/p&gt;

&lt;p&gt;Now that the logic for calling our api is done, let's initialize our &lt;code&gt;AbortController&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let abortController = new AbortController();
let signal = abortController.signal;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now to actually cancel our api call inside the &lt;code&gt;fetchQuote&lt;/code&gt; function you can add &lt;code&gt;abortController.abort()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchQuote(search) {
  try {
    // Cancel our api call if isLoading is true
    if (isLoading) {
      abortController.abort();
    }

    isLoading = true;

    // Pass the "signal" as a param to fetch
    const response = await fetch(
      `https://api.chucknorris.io/jokes/search?query=${search}`,
      { signal }
    );
    // rest of the function is same as above
  } catch(err) {
    isLoading = false;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that aborted request is cancelled, it actually goes to our &lt;code&gt;catch&lt;/code&gt; block. Since technically this isn't an error, we can bypass this by checking for abort errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;catch(err) {
  // Only handle errors if it is not an "AbortError"
  if (err.name === 'AbortError') {
    console.log('Fetch aborted');
  } else {
    console.error('Uh oh, an error!', err);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now any request we make cancels the previous request if it hasn't resolved yet. &lt;/p&gt;

&lt;p&gt;But there is a catch, this doesn't work for subsequent requests and only works for the first request. For &lt;code&gt;AbortController&lt;/code&gt; to work for all of our subsequent requests, we need to create a new one each time we abort a request. Which leaves us with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchQuote(search) {
  try {
    if (isLoading) {
      // Cancel the request
      abortController.abort();

      // Create a new instance of abortController
      abortController = new AbortController();
      signal = abortController.signal;
    }

    isLoading = true;

    const response = await fetch(
      `https://api.chucknorris.io/jokes/search?query=${search}`,
      { signal }
    );

    const data = await response.json();

    const jokes = data.result.slice(0, 5);

    isLoading = false;
    renderJokes(jokes);
  } catch (err) {
    isLoading = false;
    if (err.name === 'AbortError') {
      console.log('Fetch aborted');
    } else {
      console.error('Uh oh, an error!', err);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we are finally able to successfully use abort-able &lt;code&gt;fetch&lt;/code&gt; requests out in the wild:&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%2Fi%2F6wbw7nbdfbufltssilw3.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%2Fi%2F6wbw7nbdfbufltssilw3.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Array.sort gotcha: Case of the missing key</title>
      <dc:creator>Aviskar KC</dc:creator>
      <pubDate>Sat, 21 Mar 2020 08:49:12 +0000</pubDate>
      <link>https://dev.to/aviskarkc10/array-sort-gotcha-case-of-the-missing-key-42gk</link>
      <guid>https://dev.to/aviskarkc10/array-sort-gotcha-case-of-the-missing-key-42gk</guid>
      <description>&lt;p&gt;When you are working as a web developer, you will have to sort an array in ascending or descending order at some point in time.&lt;/p&gt;

&lt;p&gt;To sort arrays based on some value, we can use the built-in Array.prototype.sort() function which works as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sort in ascending order
array.sort(function (a, b) {
  if (a &amp;lt; b) {
     return -1;
  }

  if (a &amp;gt; b) {
     return 1;
  }

  if (a === b) {
     return 0;
  }
});

// sort in descending order
array.sort(function (a, b) {
  if (a &amp;lt; b) {
     return 1;
  }

  if (a &amp;gt; b) {
     return -1;
  }

  if (a === b) {
     return 0;
  }
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since the &lt;code&gt;array.prototype.sort()&lt;/code&gt; function depends on &lt;code&gt;positive&lt;/code&gt;, &lt;code&gt;negative&lt;/code&gt; and &lt;code&gt;zero&lt;/code&gt; values to determine the order of elements, we can simplify the above logic if we are working with numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sort in ascending order
array.sort(function (a, b) {
  return a - b;
});

// sort in descending order
array.sort(function (a, b) {
  return b - a;
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So if we are sorting an array of object, in ascending order, our code will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [{ id: 1, value: 10 }, { id: 2, value: 2 }, { id: 3, value: 5 }];

// sort in ascending order:
array.sort(function(a, b) {
  return a.value - b.value;
});

// Output =&amp;gt; [{ id: 2, value:  3 }, { id: 3, value: 5 }, { id: 1, value: 10 }];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But imagine I introduce a new element into the mix, which doesn't have the key &lt;code&gt;value&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// New element { id: 4 } which doesn't have the key `value`
let array = [{ id: 1, value: 10 }, { id: 4 }, { id: 2, value: 2 }, { id: 3, value: 5 }]; 

// sort in ascending order:
array.sort(function(a, b) {
  return a.value - b.value;
});

// Output =&amp;gt; [{ id: 1, value: 10 }, { id: 4 }, { id: 2, value: 2 }, { id: 3, value: 5 }];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because our new element doesn't have the key &lt;code&gt;value&lt;/code&gt;, accessing it will return &lt;code&gt;undefined&lt;/code&gt;. Now, trying to subtract a number with &lt;code&gt;undefined&lt;/code&gt; will result in &lt;code&gt;NaN&lt;/code&gt; and cause our sort function to not work as intended.&lt;/p&gt;

&lt;p&gt;I recently got stung by this gotcha and worked around it by assigning &lt;code&gt;0&lt;/code&gt; to the missing values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// New element { id: 4 } which doesn't have the key `value`
let array = [{ id: 1, value: 10 }, { id: 4 }, { id: 2, value: 2 }, { id: 3, value: 5 }]; 

// sort in ascending order:
array.sort(function(a, b) {
  const valueA = a.value || 0;
  const valueB = b.value || 0;

  return valueA - valueB;
});

// Output =&amp;gt; [{ id: 4 }, { id: 2, value: 2 }, { id: 3, value: 5 }, { id: 1, value: 10 }];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As I was ordering array of objects in descending order by &lt;code&gt;likes&lt;/code&gt;, this worked perfectly for my use case.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>.npmignore: A small step towards unbloating JS</title>
      <dc:creator>Aviskar KC</dc:creator>
      <pubDate>Sun, 26 Jan 2020 16:51:51 +0000</pubDate>
      <link>https://dev.to/aviskarkc10/npmignore-a-small-step-towards-unbloating-js-44n9</link>
      <guid>https://dev.to/aviskarkc10/npmignore-a-small-step-towards-unbloating-js-44n9</guid>
      <description>&lt;p&gt;Javascript bloat is a real thing. There are blog posts out there that help you migrate from a heavy library to a much smaller one while tools like &lt;a href="https://bundlephobia.com/"&gt;bundlephobia&lt;/a&gt; help you calculate the cost of adding a package to your application.&lt;/p&gt;

&lt;p&gt;As library maintainers, you can make use of &lt;code&gt;.npmignore&lt;/code&gt; to ignore unnecessary files from being included in your package which will in turn help reduce the amount of code that gets shipped to projects that use your library.&lt;/p&gt;

&lt;p&gt;Some of the things you can keep out of your package are:&lt;/p&gt;

&lt;p&gt;1) If your package only requires the final build file to work, you can keep out the &lt;code&gt;src&lt;/code&gt; folder.&lt;br&gt;
2) The &lt;code&gt;tests&lt;/code&gt; folder.&lt;br&gt;
3) The files containing demos/examples of how your library works.&lt;br&gt;
4) Your dotfiles that aren't already in &lt;code&gt;.gitignore&lt;/code&gt; like eslint/prettier configs.&lt;/p&gt;

&lt;p&gt;Ignoring these files, helped me bring down my library, &lt;a href="//github.com/aviskarkc10/react-d3-donut"&gt;react-d3-donut&lt;/a&gt;'s size (which was previously around 1MB) down to just &lt;a href="https://bundlephobia.com/result?p=react-d3-donut@1.1.2"&gt;138B&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.npmignore&lt;/code&gt; file works exactly like &lt;code&gt;.gitignore&lt;/code&gt; in terms of syntax and how you would whitelist/blacklist file. You can find &lt;em&gt;.npmignore&lt;/em&gt;'s documentation here: &lt;br&gt;
&lt;a href="https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package"&gt;https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>npm</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A bash script to create .gitignore files</title>
      <dc:creator>Aviskar KC</dc:creator>
      <pubDate>Thu, 10 Oct 2019 18:00:23 +0000</pubDate>
      <link>https://dev.to/aviskarkc10/a-bash-script-to-create-gitignore-files-30je</link>
      <guid>https://dev.to/aviskarkc10/a-bash-script-to-create-gitignore-files-30je</guid>
      <description>&lt;p&gt;Created a bash script that generates .gitignore files of your language of choosing. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/aviskarkc10/gitignore"&gt;https://github.com/aviskarkc10/gitignore&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The .gitignore files are pulled from &lt;a href="https://www.gitignore.io"&gt;https://www.gitignore.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feedback are welcome.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>git</category>
    </item>
  </channel>
</rss>
