<?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: Amit K.। 🎧</title>
    <description>The latest articles on DEV Community by Amit K.। 🎧 (@amitaldo).</description>
    <link>https://dev.to/amitaldo</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%2F268981%2F22e1e51c-9f25-43a6-b076-0407ab85dbf8.jpg</url>
      <title>DEV Community: Amit K.। 🎧</title>
      <link>https://dev.to/amitaldo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amitaldo"/>
    <language>en</language>
    <item>
      <title>"Function makes the dependencies of useEffect Hook change on every render" warning in React</title>
      <dc:creator>Amit K.। 🎧</dc:creator>
      <pubDate>Tue, 07 Mar 2023 19:17:39 +0000</pubDate>
      <link>https://dev.to/amitaldo/function-makes-the-dependencies-of-useeffect-hook-change-on-every-render-warning-in-react-35hd</link>
      <guid>https://dev.to/amitaldo/function-makes-the-dependencies-of-useeffect-hook-change-on-every-render-warning-in-react-35hd</guid>
      <description>&lt;p&gt;If you work with the useEffect hook, you have likely encountered the following warning:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The ‘functionName’ function makes the dependencies of useEffect Hook (at line X) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of ‘functionName’ in its own useCallback() Hook. (react-hooks/exhaustive-deps)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So what’s going on here and how do we fix it?&lt;/p&gt;

&lt;h2&gt;
  
  
  A contrived example
&lt;/h2&gt;

&lt;p&gt;Let’s check a quick example. We’ll have a simple react app and updateCount function which we will call inside useEffect hooks and we need to pass the function name in dependency array or it will show &lt;code&gt;React Hook useEffect has missing dependency warning&lt;/code&gt;. What we want here is every time count changes, we want to run an effect that logs the count to the console. So we write the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [count, setCount] = useState(0);

  const updateCount = () =&amp;gt; {
    console.log(count);
  };

  useEffect(() =&amp;gt; {
    updateCount();
  }, [updateCount]);

  return &amp;lt;div&amp;gt;{count}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we see the warning! So, lets use it to explore what’s happening.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency arrays and referential equality
&lt;/h2&gt;

&lt;p&gt;In our example, our useEffect hook has the updateCount function in its dependency array. That means the effect will run every time after updateCount changes.&lt;/p&gt;

&lt;p&gt;You might be thinking that updateCount is a function and it doesn’t really changes after each render. But we have to remember that JavaScript equality works based on referential equality. According to the principle of Object.is(), any two objects pointing to the exact memory location are considered equal or only equal to each other if they reference the same object in memory. That’s why when we compare two objects with same key and value we end up with something like this:&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({ name: 'foo' } === { name: 'foo' });
// false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two objects live separately in memory and are therefore not equal.&lt;/p&gt;

&lt;p&gt;This works the same way for functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fn1 = () =&amp;gt; 'bar';
const fn2 = () =&amp;gt; 'bar';

console.log(fn1 === fn2);
// false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is what’s happening in our useEffect dependency array. The updateCount function is recreated on each render and so it be will always be different than the updateCount function created during the previous render.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to fix this?
&lt;/h2&gt;

&lt;p&gt;Well, the warning message is actually pretty helpful and itself gives us the solutions that will help us fix the problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move updateCount inside the useEffect hook&lt;/li&gt;
&lt;li&gt;Wrap updateCount in a useCallback hook&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ll show you both solution here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Moving updateCount inside the useEffect hook&lt;/strong&gt;&lt;br&gt;
This one is fairly straightforward: we take the updateCount function and move it inside the useEffect hook. Now the  updateCount is now inside the useEffect hook, we no longer need it in the dependency array. However, since updateCount depends on the count variable, we’ll have to add the count variable to the dependency array.&lt;/p&gt;

&lt;p&gt;Our fixed code now looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [count, setCount] = useState(1);

  useEffect(() =&amp;gt; {
    const updateCount = () =&amp;gt; {
      console.log(count);
    };
    updateCount();
  }, [count]);

  return &amp;lt;div className="App"&amp;gt;{count}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the warning is gone!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wrapping updateCount in a useCallback hook&lt;/strong&gt;&lt;br&gt;
Let's consider a situation in which you need updateCount to call elsewhere in the component, putting it inside the effect could cause it to be undefined elsewhere.&lt;/p&gt;

&lt;p&gt;For this solution we need to wrap the updateCount function definition in a useCallback hook. What this does is returns a memoized function whose reference will only change if something in the hook’s dependency array changes.&lt;/p&gt;

&lt;p&gt;Let’s take a look at the correct implementation:&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, useCallback } from 'react';

function App() {
  const [count, setCount] = useState(1);

  const updateCount = useCallback(() =&amp;gt; {
    console.log(count);
  }, [count]);

  useEffect(() =&amp;gt; {
    updateCount();
  }, [updateCount]);

  return &amp;lt;div className="App"&amp;gt;{count}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have wrapped our updateCount function in a useCallback hook, it will maintain the same memory reference through each render—unless something in its dependency array changes. In this case, we added count to the dependency array. We need the function to update when count updates or our effect won’t run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;React dependency array issues can be pretty tricky. But, there’s always a solution!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title># Basic Linux Commands List</title>
      <dc:creator>Amit K.। 🎧</dc:creator>
      <pubDate>Mon, 18 Nov 2019 19:21:34 +0000</pubDate>
      <link>https://dev.to/amitaldo/basic-linux-commands-list-3el0</link>
      <guid>https://dev.to/amitaldo/basic-linux-commands-list-3el0</guid>
      <description>&lt;p&gt;You might already know a few of these Linux command tips or perhaps all of it. In either case, you are welcome to share your favorite tricks in the comment section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Use the Command Line?
&lt;/h3&gt;

&lt;p&gt;Both the GUI(Graphical User Interface) and CLI(Command Line Interface) are ways you can interact with your computer.&lt;br&gt;
But when it comes to the command line, once you learn the commands, it is faster and more powerful to navigate and use your computer. This is why many programmers prefer the command line.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Flags?
&lt;/h3&gt;

&lt;p&gt;Most command line utilities take parameters using flags. Flags usually come in short form &lt;em&gt;(-h)&lt;/em&gt; and long form &lt;em&gt;(--help)&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Quick note :&lt;/strong&gt; I’m only listing the most frequent uses here. If you want to learn more, you can use your terminal to see the manual.&lt;br&gt;
&lt;br&gt; &amp;gt; &lt;strong&gt;For that type &lt;code&gt;man command_name&lt;/code&gt; in your terminal&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'users' Command
&lt;/h4&gt;

&lt;p&gt;Users command displays currently logged in users.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;users&lt;/code&gt;
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'date' Command
&lt;/h4&gt;

&lt;p&gt;Date command displays current date and time in the terminal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;date -u&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Tue Nov 12 18:19:59 UTC 2019&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'cal' Command
&lt;/h4&gt;

&lt;p&gt;Cal command displays the calendar of current month in the terminal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cal&lt;/code&gt;
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cal 'year'&lt;/code&gt; display calendar of that particular year
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cal -m 'month name'&lt;/code&gt; display calendar of a particular month of that year
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cal -m 'month name' 'year'&lt;/code&gt; display calendar of a particular month of a particular year
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k9ydBdyJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/p6jtlpkm0qcnyp7juspu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k9ydBdyJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/p6jtlpkm0qcnyp7juspu.png" alt="Alt Text" width="734" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  'pwd' Command
&lt;/h4&gt;

&lt;p&gt;pwd command displays the full path of present working directory in the terminal.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IAcY9LP4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/q45dd04ve1tg99c7hx06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IAcY9LP4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/q45dd04ve1tg99c7hx06.png" alt="Alt Text" width="729" height="60"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  'cd' Command
&lt;/h4&gt;

&lt;p&gt;cd command is used to navigate through the folders using the terminal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cd 'folder name'&lt;/code&gt; switches to folder with name folder-name
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cd ..&lt;/code&gt; takes you to the absolute path
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cd&lt;/code&gt; or &lt;code&gt;cd ~&lt;/code&gt; home directory
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cd -&lt;/code&gt; previous working directory
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'mkdir' Command
&lt;/h4&gt;

&lt;p&gt;mkdir command is used to navigate through the folders in the terminal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;mkdir &amp;lt;directory name&amp;gt;&lt;/code&gt; to make directory
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;mkdir &amp;lt;dir1&amp;gt; &amp;lt;dir2&amp;gt; &amp;lt;dir3&amp;gt;&lt;/code&gt; to create multiple directories
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;mkdir -p &amp;lt;dir1&amp;gt;/&amp;lt;dir2&amp;gt;&lt;/code&gt; to create intermediate directories
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'ls' Command
&lt;/h4&gt;

&lt;p&gt;ls is a simple command to list all the files in current directory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;ls -l&lt;/code&gt; show detailed info in columns
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;ls -a&lt;/code&gt; show the hidden file also
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;ls -t&lt;/code&gt; sort by last modified date newest first
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;ls -S&lt;/code&gt; sort by size
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;ls -R&lt;/code&gt; recursively list the directories with the subdirectories
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'cp' Command
&lt;/h4&gt;

&lt;p&gt;cp is a simple command to copy paste the files from one directory to another directory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cp &amp;lt;item1&amp;gt; &amp;lt;item2&amp;gt;&lt;/code&gt; copies the single file/directory item1 to the file/directory item2
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cp file1 file2 dir1&lt;/code&gt; copy file1 and file2 into directory dir1
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cp dir1/* dir2&lt;/code&gt; using a wildcard, copy all the files in dir1 into dir2
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;cp r dir1 dir2&lt;/code&gt; Copy the contents of directory dir1 to directory dir2
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'mv' Command
&lt;/h4&gt;

&lt;p&gt;mv is a simple command to move or rename the files, it performs both operations file moving and file renaming&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;mv &amp;lt;item1&amp;gt; &amp;lt;item2&amp;gt;&lt;/code&gt; Move file1 to file2. If file2 exists, it is overwritten with the contents of file1. If file2 does not exist, it is created.
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  'rm' Command
&lt;/h4&gt;

&lt;p&gt;rm is a simple command to delete files.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;rm &amp;lt;item&amp;gt;&lt;/code&gt; used to remove (delete) files and directories
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;rm -r dir1&lt;/code&gt; delete dir1 and its contents.
&lt;/h3&gt;
&lt;h3&gt;
  
  
  # &lt;code&gt;rm -r file1 dir1&lt;/code&gt; delete file1 and dir1 and its contents
&lt;/h3&gt;
&lt;/blockquote&gt;

</description>
      <category>linux</category>
      <category>codenewbie</category>
      <category>ubuntu</category>
    </item>
  </channel>
</rss>
