<?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: Amandeep Singh</title>
    <description>The latest articles on DEV Community by Amandeep Singh (@amand33p).</description>
    <link>https://dev.to/amand33p</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%2F330062%2F11a6d84f-1bc4-45cb-88f3-17b6cd884ed5.jpeg</url>
      <title>DEV Community: Amandeep Singh</title>
      <link>https://dev.to/amand33p</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amand33p"/>
    <language>en</language>
    <item>
      <title>Some Custom React Hooks I've used so far.</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Wed, 12 May 2021 14:34:36 +0000</pubDate>
      <link>https://dev.to/amand33p/my-favorite-custom-react-hooks-337o</link>
      <guid>https://dev.to/amand33p/my-favorite-custom-react-hooks-337o</guid>
      <description>&lt;p&gt;Hi there,&lt;br&gt;
As we all know, ReactJS is currently the most popular front-end framework. Some years back, a powerful feature was introduced by React devs - &lt;strong&gt;React Hooks&lt;/strong&gt;, where they moved from class-based programming approach to function-based, which turned out to be a game changer! &lt;/p&gt;

&lt;p&gt;In addition to the in-built hooks from React's, like useState, useEffect, useContext etc., devs can write their own hooks - termed as Custom Hooks.&lt;/p&gt;

&lt;p&gt;In this post, I'm gonna mention my favorite custom hooks, which I found useful for my projects.&lt;/p&gt;
&lt;h2&gt;
  
  
  useLocalStorage:
&lt;/h2&gt;

&lt;p&gt;The usage is similar to &lt;strong&gt;useState&lt;/strong&gt;, but instead of storing the value in virtual state, we store it in browser's local storage and the value is persisted on page refresh.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
If your site has dark theme mode, you can use the hook to persist theme's switch. So, whenever user returns to your site, it'll apply the theme automatically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://usehooks.com/useLocalStorage/"&gt;Source code &amp;amp; usage&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  useMediaQuery
&lt;/h2&gt;

&lt;p&gt;If you're aware of media queries from CSS, you'll get the usage info. from the name itself. Basically, you can use this when you want to conditionally render something according to device-screen's width.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
Without using CSS, you can un-render mobile menu component, whenever screen size exceeds 768px.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.30secondsofcode.org/react/s/use-media-query"&gt;Source code &amp;amp; usage&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  useClickOutside
&lt;/h2&gt;

&lt;p&gt;With this hook, you can detect click events outside of a particular element.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
If you have a modal/dialog component, you want it to close whenever user clicks outside of the main element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://usehooks.com/useOnClickOutside/"&gt;Source code &amp;amp; usage&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  useScrollDown
&lt;/h2&gt;

&lt;p&gt;This hook will return true, if the scroll hits the specified value.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
If you have a floating button for scroll up, but you only want it to show when your page is scrolled down a bit from initial position.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from 'react';

const useScrollDown = (height) =&amp;gt; {
  const [boolean, setBoolean] = useState(false);

  const handler = () =&amp;gt; {
    if (window.pageYOffset &amp;gt;= height) {
      setBoolean(true);
    } else {
      setBoolean(false);
    }
  };

  useEffect(() =&amp;gt; {
    window.addEventListener('scroll', handler);
    return () =&amp;gt; window.removeEventListener('scroll', handler);
  }, []);

  return boolean;
};

export default useScrollDown;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const isScrollDown = useScrolldown(90);

return (
  &amp;lt;div&amp;gt;
    {isScrollDown ? "It's down!" : "Initial position."}
  &amp;lt;/div&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






</description>
      <category>react</category>
      <category>javascript</category>
      <category>hooks</category>
    </item>
    <item>
      <title>The big 3 of array methods - map, filter &amp; reduce</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Fri, 30 Apr 2021 14:56:38 +0000</pubDate>
      <link>https://dev.to/amand33p/the-big-3-of-array-methods-map-filter-reduce-26oc</link>
      <guid>https://dev.to/amand33p/the-big-3-of-array-methods-map-filter-reduce-26oc</guid>
      <description>&lt;p&gt;Hi there,&lt;br&gt;
These array methods are the most important part of functional programming in JavaScript. I'm pretty sure if you're building a project in JS, you're gonna use atleast one of these, if not all.&lt;/p&gt;

&lt;p&gt;So, let's get started!&lt;/p&gt;

&lt;p&gt;Sample array:&lt;br&gt;
&lt;code&gt;const arr = [2, 4, 6, 8, 10]&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  map()
&lt;/h2&gt;

&lt;p&gt;It creates a new array with the results of calling a function for every array element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapped = arr.map((element, index) =&amp;gt; element * index);
//creates an array, multiplying every element of the original array with its index.

console.log(mapped);
// Outputs: [0, 4, 12, 24, 40]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  filter()
&lt;/h2&gt;

&lt;p&gt;It creates a new array filled with all array elements that pass a test (provided as a function).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const filtered = arr.filter((element, index) =&amp;gt; element % 4 === 0);
//creates an array, filtering the original array with elements divisible by 4. 

console.log(filtered);
// Outputs: [4, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  reduce()
&lt;/h2&gt;

&lt;p&gt;It reduces the array to a single value, executing a provided function for each value of the array (from left-to-right).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reduced = arr.reduce((sum, current) =&amp;gt; sum + current, 0);
//calculates sum of all the array elements

console.log(reduced);
// Outputs: 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>methods</category>
    </item>
    <item>
      <title>call(), apply() &amp; bind() in JavaScript</title>
      <dc:creator>Amandeep Singh</dc:creator>
      <pubDate>Thu, 29 Apr 2021 11:43:02 +0000</pubDate>
      <link>https://dev.to/amand33p/call-apply-bind-in-javascript-264g</link>
      <guid>https://dev.to/amand33p/call-apply-bind-in-javascript-264g</guid>
      <description>&lt;p&gt;Hi there,&lt;br&gt;
In this post, I'm gonna talk about the function methods mentioned in the title. A lot of new JavaScript devs find them difficult to understand, so here I am!&lt;/p&gt;

&lt;p&gt;Here's our object for example purposes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const person = { name: "Aman" }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The function which we want to invoke as object method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const intro = function(profession, country) {
  return `${this.name} is a ${profession}, who lives in ${country}.`
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  call()
&lt;/h2&gt;

&lt;p&gt;call() is used to invoke a function as method, where first parameter refers to the "this" for the object, remaining parameters acts as regular function parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(intro.call(person, "Developer", "India"));
// Outputs: "Aman is a Developer, who lives in India."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  apply()
&lt;/h2&gt;

&lt;p&gt;apply() is similar to call(), only difference is the way function parameters are passed i.e. in an array, instead of comma-separated arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(intro.apply(person, ["Developer", "India"]));
// Outputs: "Aman is a Developer, who lives in India."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  bind()
&lt;/h2&gt;

&lt;p&gt;bind() is different compared to call() &amp;amp; apply(). When we pass the "this" of the object as argument, it returns a bound function which we can invoke later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const boundIntro = intro.bind(person);
console.log(boundIntro("Developer", "India"));
// Outputs: "Aman is a Developer, who lives in India."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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