<?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: Alwaz Qazi</title>
    <description>The latest articles on DEV Community by Alwaz Qazi (@alwaz).</description>
    <link>https://dev.to/alwaz</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%2F722539%2Fc6e65803-1928-4d73-b1b3-907c8673d4d3.png</url>
      <title>DEV Community: Alwaz Qazi</title>
      <link>https://dev.to/alwaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alwaz"/>
    <language>en</language>
    <item>
      <title>Optimizing React Performance with useDebounce.</title>
      <dc:creator>Alwaz Qazi</dc:creator>
      <pubDate>Sat, 23 Dec 2023 16:57:59 +0000</pubDate>
      <link>https://dev.to/alwaz/optimizing-react-performance-with-usedebounce-37ah</link>
      <guid>https://dev.to/alwaz/optimizing-react-performance-with-usedebounce-37ah</guid>
      <description>&lt;p&gt;The term “&lt;strong&gt;debounce&lt;/strong&gt;” might sound technical, but its concept is like a traffic light for signals in the world of software development. Imagine you’re at a busy intersection, and there’s a button to cross the road. If you press and release it quickly, the system might get confused, thinking you pressed it multiple times.&lt;/p&gt;

&lt;p&gt;Debouncing is giving the system a short break. Picture the traffic light saying, “Wait a sec, let’s make sure the person wants to cross before changing the signal again” In software development, this concept is crucial when dealing with heavy tasks, such as numerous API calls, where efficiency is key.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Initial Approach
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";
import { getUsers } from "./api";
import User from "./type";
import "./styles.css";

export default function App2() {
  const [search, setSearch] = useState&amp;lt;string&amp;gt;("");
  const [loading, setLoading] = useState&amp;lt;boolean&amp;gt;(false);
  const [user, setUser] = useState&amp;lt;User[]&amp;gt;([]);

  useEffect(() =&amp;gt; {
    const loadUsers = async () =&amp;gt; {
      setLoading(true);
      const users = await getUsers(search);
      setUser(users?.users);
      setLoading(false);
    };

    loadUsers();
  }, [search]);

  const handleSearch = (e: React.ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt; {
    setSearch(e.target.value);
  };

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;input type="text" value={search} onChange={handleSearch} /&amp;gt;
      {loading ? (
        &amp;lt;&amp;gt;Loading...&amp;lt;/&amp;gt;
      ) : (
        user.map((user) =&amp;gt; &amp;lt;h2 key={user.id}&amp;gt;{user?.firstName}&amp;lt;/h2&amp;gt;)
      )}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let’s take a look at a React code snippet above that searches a list of users based on user input. While this code works as you can see in the output below, it listens to every keystroke, leading to potentially unnecessary API calls and reduced performance.&lt;br&gt;
&lt;/p&gt;
&lt;div&gt;
  &lt;iframe src="https://loom.com/embed/c308d5d28e604c6aa65e34d6bf57c76c"&gt;
  &lt;/iframe&gt;
&lt;/div&gt;



&lt;h3&gt;
  
  
  Introducing Debounce for Performance
&lt;/h3&gt;

&lt;p&gt;To tackle this issue, we introduced the concept of debounce and created a custom hook, useDebounce, to delay updates to a value until a specified time has passed since the last update.&lt;br&gt;
&lt;/p&gt;

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

export const useDebouce = &amp;lt;T&amp;gt;(value: T, delay = 500) =&amp;gt; {
  const [deboucedValue, setDebouncedValue] = useState&amp;lt;T&amp;gt;(value);

  useEffect(() =&amp;gt; {
    const timeout = setTimeout(() =&amp;gt; {
      setDebouncedValue(value);
    }, delay);

    return () =&amp;gt; clearTimeout(timeout);
  }, [value, delay]);

  return deboucedValue;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, using this custom hook to our React component.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";
import { getUsers } from "./api";
import User from "./type";
import { useDebouce } from "./hooks";
import "./styles.css";

export default function App() {
  const [search, setSearch] = useState&amp;lt;string&amp;gt;("");
  const [loading, setLoading] = useState&amp;lt;boolean&amp;gt;(false);
  const [user, setUser] = useState&amp;lt;User[]&amp;gt;([]);
  const debouncedSearch = useDebouce(search);

  useEffect(() =&amp;gt; {
    const loadUsers = async () =&amp;gt; {
      setLoading(true);
      const users = await getUsers(debouncedSearch);
      setUser(users?.users);
      setLoading(false);
    };

    loadUsers();
  }, [debouncedSearch]);

  const handleSearch = (e: React.ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt; {
    setSearch(e.target.value);
  };

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;input type="text" value={search} onChange={handleSearch} /&amp;gt;
      {loading ? (
        &amp;lt;&amp;gt;Loading...&amp;lt;/&amp;gt;
      ) : (
        user.map((user) =&amp;gt; &amp;lt;h2 key={user.id}&amp;gt;{user?.firstName}&amp;lt;/h2&amp;gt;)
      )}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;we can observe a significant improvement as you can see in the output video below. The API call is triggered only when the user stops typing, enhancing the overall efficiency of the app.&lt;/p&gt;


&lt;div&gt;
  &lt;iframe src="https://loom.com/embed/bc71f399c02b41c89af83ba35010158d"&gt;
  &lt;/iframe&gt;
&lt;/div&gt;



&lt;h3&gt;
  
  
  Why It Matters?
&lt;/h3&gt;

&lt;p&gt;Implementing search functionality is a common requirement in applications. Humans typically type more than one character every 500ms. By applying debounce, we can reasonably assume that the user has finished typing if there’s been no input for 500ms, optimizing the application’s performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In conclusion, understanding and applying debounce can greatly enhance the performance of your React applications. By incorporating the &lt;code&gt;useDebounce&lt;/code&gt; hook, you can strike a balance between responsiveness and efficiency, ensuring a smoother user experience.&lt;/p&gt;

&lt;p&gt;Try implementing debounce in your projects and share your experiences. The optimization possibilities are vast, and by mastering these concepts, you empower yourself to build more performant and responsive applications.&lt;/p&gt;




&lt;p&gt;Connect with me to stay updated on more tips and tricks for efficient coding! Follow me on &lt;a href="https://github.com/Alwaz"&gt;GitHub&lt;/a&gt; for the latest repositories and projects. Let’s connect on &lt;a href="https://www.linkedin.com/in/alwaz-qazi/"&gt;LinkedIn&lt;/a&gt; to share insights and collaborate on exciting ventures.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;blockquote&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>performance</category>
      <category>debounce</category>
      <category>optimisation</category>
    </item>
    <item>
      <title>JavaScript “Promises” — Simply Explained</title>
      <dc:creator>Alwaz Qazi</dc:creator>
      <pubDate>Tue, 18 Jan 2022 16:16:38 +0000</pubDate>
      <link>https://dev.to/alwaz/javascript-promises-simply-explained-28d</link>
      <guid>https://dev.to/alwaz/javascript-promises-simply-explained-28d</guid>
      <description>&lt;p&gt;JavaScript promises are same as the promises we make in real life. The commitment we make to someone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; &lt;em&gt;“I promise I will code daily.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now this promise has two results.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You either fulfill it (resolved).&lt;/li&gt;
&lt;li&gt;or You’ll break it. (rejected).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let’s have a look at the syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
Promise syntax is super easy just like how we create a constructor. But here we pass a function with two parameters resolved(value) and reject(error).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f3VLV5Q3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/px4j7fayeytfy6ktq8b3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f3VLV5Q3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/px4j7fayeytfy6ktq8b3.png" alt="Image description" width="488" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ik7c2O6k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/924pffl8wwhx8zoqzwe4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ik7c2O6k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/924pffl8wwhx8zoqzwe4.png" alt="Image description" width="509" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we've made a promise that value of a=1+1 should be 2. If it is 2 then promise is resolved else it is rejected.&lt;/p&gt;

&lt;p&gt;Now, let's learn how we can interact with these promises.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F-u10p9k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c1um63e4yawl0v8eunj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F-u10p9k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4c1um63e4yawl0v8eunj.png" alt="Image description" width="470" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we will use two methods to check whether our promise has resolved or rejected.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;then()&lt;/strong&gt; will return resolved.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;catch()&lt;/strong&gt; will return rejected. as a callback message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benefits of using "Promises".&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Improves code readability.&lt;/li&gt;
&lt;li&gt;Better handling of Async operations.&lt;/li&gt;
&lt;li&gt;Provides a better flow of control while defining async logic.&lt;/li&gt;
&lt;li&gt;Better error handling.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope you got the concept of how Promises work. If you did please follow and share.&lt;/p&gt;

&lt;p&gt;Even if you didn't, that's fine you can do it anyway .😄&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
