<?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: Amr Guaily</title>
    <description>The latest articles on DEV Community by Amr Guaily (@amrguaily).</description>
    <link>https://dev.to/amrguaily</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%2F726684%2F6fd9a9c1-93fd-4cdd-9a74-85b2eeb7d155.jpg</url>
      <title>DEV Community: Amr Guaily</title>
      <link>https://dev.to/amrguaily</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amrguaily"/>
    <language>en</language>
    <item>
      <title>DOM vs Virtual DOM</title>
      <dc:creator>Amr Guaily</dc:creator>
      <pubDate>Mon, 06 Mar 2023 11:47:31 +0000</pubDate>
      <link>https://dev.to/amrguaily/dom-vs-virtual-dom-2d3p</link>
      <guid>https://dev.to/amrguaily/dom-vs-virtual-dom-2d3p</guid>
      <description>&lt;h2&gt;
  
  
  ⏩ What is DOM❓
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DOM&lt;/strong&gt; stands for =&amp;gt; &lt;strong&gt;D&lt;/strong&gt;ocument &lt;strong&gt;O&lt;/strong&gt;bject &lt;strong&gt;M&lt;/strong&gt;odal&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DOM&lt;/strong&gt; is a &lt;strong&gt;Programming Interface&lt;/strong&gt; for the HTML document that allows us to create, change, add, or remove elements from the HTML document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DOM&lt;/strong&gt; is represented as a &lt;strong&gt;tree&lt;/strong&gt; data structure.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F5r3qhr1x14o0gjo6qell.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5r3qhr1x14o0gjo6qell.png" alt="DOM tree" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🤔 At this point you may be wondering, and ask yourself &lt;strong&gt;Why can't update HTML document directly.. Why DOM is Required ❗❓&lt;/strong&gt;&lt;br&gt;
✅ We need DOM because &lt;u&gt;JS doesn't natively understand HTML document.&lt;/u&gt; So the browser creates DOM. therefore we could say that &lt;strong&gt;DOM is an API that represents the same HTML document but in different format (object format).&lt;/strong&gt; &lt;br&gt;
For example: JS can't understand the &lt;code&gt;&amp;lt;h1&amp;gt;&amp;lt;/h1&amp;gt;&lt;/code&gt; tag in HTML documents ,it only understand h1 object in DOM.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating DOM is Expensive, WHY ❗❓
&lt;/h3&gt;

&lt;p&gt;✅ Whenever DOM changes it causes browser to run two expensive operations: &lt;a href="https://dev.to/gopal1996/understanding-reflow-and-repaint-in-the-browser-1jbg"&gt;&lt;strong&gt;Reflow and Repaint&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Because of how updating DOM is expensive - React is using &lt;strong&gt;Virtual DOM.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  ⏩ What is Virtual DOM❓
&lt;/h2&gt;

&lt;p&gt;React uses virtual DOM which is a &lt;strong&gt;lightweight copy of the actual DOM&lt;/strong&gt;, the only difference is the ability to directly change the layout of the document.&lt;/p&gt;

&lt;h3&gt;
  
  
  How DOM updates in React❓
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;On the first run, both virtual DOM and real DOM are created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Whenever state changes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
React creates a new virtual Dom and compare it with the previous one.&lt;/li&gt;
&lt;li&gt;
The compression is done by &lt;strong&gt;Diffing Algorithm.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; 
After compering react creates a new virtual Dom with the changes.&lt;/li&gt;
&lt;li&gt;
Then &lt;u&gt;&lt;strong&gt;React updates the real DOM without rendering the entire DOM&lt;/strong&gt;&lt;/u&gt;, and this is how virtual DOM is helping to enhance performance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;This entire process of transforming changes to the real DOM is called &lt;a href="https://www.geeksforgeeks.org/reactjs-reconciliation/" rel="noopener noreferrer"&gt;&lt;strong&gt;Reconciliation&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  🤔 Suppose we have new data similar to the previous one?
&lt;/h3&gt;

&lt;p&gt;✅ In that case React compares previous and new structures and sees that it has no change, so nothing gets rendered to the Browser. This is how the Virtual DOM is helping to enhance the Browser performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  🤔 Is Virtual DOM the only way to reduce costly DOM operations?
&lt;/h3&gt;

&lt;p&gt;✅ Not necessarily, other frameworks like ember.js, angular, and svelte uses different approaches to solve the very same problem.&lt;/p&gt;




&lt;h3&gt;
  
  
  ⏩ Recap:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F03ilzb6s27r8tglqu2j5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F03ilzb6s27r8tglqu2j5.png" alt="DOM vs Virtual DOM" width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  ⏩ Learn Further:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/reactjs-virtual-dom/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/reactjs-virtual-dom/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/gopal1996/understanding-reflow-and-repaint-in-the-browser-1jbg"&gt;https://dev.to/gopal1996/understanding-reflow-and-repaint-in-the-browser-1jbg&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/reactjs-reconciliation/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/reactjs-reconciliation/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>useEffect: some issues with data fetching in Effects?</title>
      <dc:creator>Amr Guaily</dc:creator>
      <pubDate>Mon, 24 Oct 2022 15:48:54 +0000</pubDate>
      <link>https://dev.to/amrguaily/useeffect-some-issues-with-data-fetching-in-effects-21nn</link>
      <guid>https://dev.to/amrguaily/useeffect-some-issues-with-data-fetching-in-effects-21nn</guid>
      <description>&lt;p&gt;In this tutorial we will discuss some issues with data fetching in &lt;code&gt;useEffect&lt;/code&gt;:&lt;br&gt;
&lt;strong&gt;1.&lt;/strong&gt; Race Conditions&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; No Caching&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; No content in initial HTML&lt;br&gt;
&lt;strong&gt;4.&lt;/strong&gt; Slow network waterfalls.&lt;/p&gt;




&lt;h2&gt;
  
  
  1️⃣ Race Condition &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You have a component that accepts an &lt;code&gt;id&lt;/code&gt; as a prop, uses that &lt;code&gt;id&lt;/code&gt; to fetch corresponding data and display it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fm286eyas6zix3e5z31fs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm286eyas6zix3e5z31fs.png" alt="code snippet" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I made a &lt;a href="https://codesandbox.io/s/race-condition-rekfni?file=/src/FetchData.js" rel="noopener noreferrer"&gt;Sandbox &lt;/a&gt;. to make it more clear for you, And i also use &lt;code&gt;setTimeOut()&lt;/code&gt; to add random wait period.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Now, Where is the Problem&lt;/strong&gt;&lt;/u&gt; ⁉&lt;br&gt;
If you rapidly click the &lt;code&gt;Fetch data!&lt;/code&gt; button several times. The app will make several requests with different &lt;code&gt;ids&lt;/code&gt; which will finish randomly out of order. And &lt;strong&gt;as a user, I expect to see the result of the last request.&lt;/strong&gt; but this may not be the case, our app displays a different result depending on which request completes first. &lt;a href="https://codesandbox.io/s/race-condition-rekfni?file=/src/FetchData.js" rel="noopener noreferrer"&gt;Try it yourself&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For Example:&lt;/strong&gt; Here I expect to see the result of last request which &lt;code&gt;id == 58&lt;/code&gt;, but the request with &lt;code&gt;id == 10&lt;/code&gt; finishes after the last request, So our app display its result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fuxl1nq7wwui3ckd4luhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fuxl1nq7wwui3ckd4luhw.png" alt="code snippet" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;How can I fix Race Conditions&lt;/u&gt; ⁉&lt;/strong&gt;&lt;br&gt;
To fix the race condition, we need to add a &lt;strong&gt;clean-up function to ignore stale responses&lt;/strong&gt; - There are 2 main approaches:&lt;br&gt;
&lt;strong&gt;1.&lt;/strong&gt; Use Boolean flag.&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Use AbortController. &lt;/p&gt;

&lt;h3&gt;
  
  
  ⏩ Clean-up Function with Boolean Flag
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fcgstpwgblcyeg1qc92u6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fcgstpwgblcyeg1qc92u6.png" alt="code snippet" width="800" height="553"&gt;&lt;/a&gt;&lt;br&gt;
According to React's official &lt;a href="https://reactjs.org/docs/hooks-reference.html#cleaning-up-an-effect" rel="noopener noreferrer"&gt;Docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If a component renders multiple times &lt;strong&gt;The previous effect is cleaned up before executing the next effect.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;So, In the above code:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
• Changing &lt;code&gt;id&lt;/code&gt; will cause a re-render.&lt;/p&gt;

&lt;p&gt;• On every re-render the clean-up function gets run first to clean-up effects from the previous render before executing the next effect.&lt;/p&gt;

&lt;p&gt;• &lt;code&gt;ignore&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt; in the clean-up function, so the previous requests won't be able to update our state.&lt;/p&gt;

&lt;p&gt;• Now, We still send a request with each click on &lt;code&gt;fetch data!&lt;/code&gt; button, But only the results from the last request will be used. check out the code in &lt;a href="https://codesandbox.io/s/clean-up-function-with-boolean-flag-7bg3sy" rel="noopener noreferrer"&gt;snadbox&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ⏩ Clean-up Function with AbortController
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F5vc067io8igkotoo1xjx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5vc067io8igkotoo1xjx.png" alt="code snippet" width="800" height="711"&gt;&lt;/a&gt;&lt;br&gt;
&lt;u&gt;&lt;strong&gt;In the above code:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
• We initialize a new &lt;code&gt;AbortController&lt;/code&gt; instance.&lt;/p&gt;

&lt;p&gt;• Then pass the &lt;code&gt;signal&lt;/code&gt; from the controller to our &lt;code&gt;fetch&lt;/code&gt; as a signal property.&lt;/p&gt;

&lt;p&gt;• As we did before - call the &lt;code&gt;abort&lt;/code&gt; function inside the clean-up function. check out the code in &lt;a href="https://codesandbox.io/s/clean-up-function-with-abrotcontroller-tqs5lc?file=/src/FetchData.js" rel="noopener noreferrer"&gt;snadbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✅ Using AbortController is the preferred way to cancel API requests, because it avoids wasting user bandwidth.&lt;/p&gt;




&lt;h2&gt;
  
  
  2️⃣ No Caching &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;🧐 &lt;u&gt;&lt;strong&gt;Problem:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
There is no-caching, so if the user navigate to another page and then clicks back, He will see the loading spinner again until the data is re-fetched. And this would be a bad user experience.&lt;/p&gt;

&lt;p&gt;🤔 &lt;u&gt;&lt;strong&gt;What we want?&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
When the user navigate to another page and then clicks back, He Should see the previous screen instantly instead of a spinner.&lt;/p&gt;

&lt;p&gt;✅ &lt;u&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
We can use third-party solution like &lt;a href="https://tanstack.com/query/v4/?from=reactQueryV3&amp;amp;original=https://react-query-v3.tanstack.com/" rel="noopener noreferrer"&gt;React Query&lt;/a&gt; or &lt;a href="https://swr.vercel.app/" rel="noopener noreferrer"&gt;useSwr&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3️⃣ No content in initial HTML &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;🧐 &lt;u&gt;&lt;strong&gt;Problem:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
Fetching in &lt;code&gt;useEffect&lt;/code&gt; means React components render, then fetch.&lt;br&gt;
&lt;a href="https://media2.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%2F0cqka9ck2wldky5511nh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0cqka9ck2wldky5511nh.png" alt="Code Snippet" width="800" height="238"&gt;&lt;/a&gt;&lt;br&gt;
&lt;u&gt;&lt;strong&gt;In the above code Snippet:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
• The function passed to &lt;code&gt;useEffect&lt;/code&gt; will be executed after the component renders. So this means: In the initial render the data is null.&lt;/p&gt;

&lt;p&gt;• Then, the effect function will be executed and update state with actual data.&lt;/p&gt;

&lt;p&gt;Since React is a Clint-Side JS library, all these renders happens on The browser. As a result the initial HTML is pretty empty. And if you try to use server-side rendering, you'll notice that &lt;strong&gt;&lt;code&gt;useEffect&lt;/code&gt; doesn't run on the server.&lt;/strong&gt; So we will end with a big problem with SEO..&lt;/p&gt;

&lt;p&gt;🤔 &lt;u&gt;&lt;strong&gt;What we want?&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
We want to pre-render the content of a page on the server instead of on the Browser.&lt;/p&gt;

&lt;p&gt;✅ &lt;u&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
We can use &lt;a href="https://nextjs.org" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt;, Next.js has Built-in server-side rendering(SSR).&lt;/p&gt;




&lt;h2&gt;
  
  
  4️⃣ Slow network waterfalls &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;🧐 &lt;u&gt;&lt;strong&gt;Problem:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
If you have parent and child components both doing fetching in &lt;code&gt;useEffect&lt;/code&gt;, then the child component can't even start fetching until the parent component finishes fetching. And This can lead to slow network waterfalls.&lt;/p&gt;

&lt;p&gt;🤔 &lt;u&gt;&lt;strong&gt;What we want?&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
The child component doesn’t have to wait for its parent to finish fetching their data before it can start.&lt;/p&gt;

&lt;p&gt;✅ &lt;u&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
Stop fetching in child component. Instead, "lift" fetch calls to a parent so they can be handled in parallel via &lt;code&gt;promise.all&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Then, pass the data down via props.&lt;br&gt;
&lt;a href="https://media2.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%2Fjtsn9cilbjm4rfr886hd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjtsn9cilbjm4rfr886hd.png" alt="Render as you fetch" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Main Resources
&lt;/h2&gt;

&lt;p&gt;• &lt;a href="https://beta.reactjs.org/learn/you-might-not-need-an-effect#fetching-data" rel="noopener noreferrer"&gt;https://beta.reactjs.org/learn/you-might-not-need-an-effect#fetching-data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;• &lt;a href="https://www.reddit.com/r/reactjs/comments/vi6q6f/comment/iddrjue" rel="noopener noreferrer"&gt;https://www.reddit.com/r/reactjs/comments/vi6q6f/comment/iddrjue&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;• &lt;a href="https://www.youtube.com/watch?v=QQYeipc_cik" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=QQYeipc_cik&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;• &lt;a href="https://maxrozen.com/race-conditions-fetching-data-react-with-useeffect" rel="noopener noreferrer"&gt;https://maxrozen.com/race-conditions-fetching-data-react-with-useeffect&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Hash Tables - full guide</title>
      <dc:creator>Amr Guaily</dc:creator>
      <pubDate>Thu, 20 Oct 2022 15:49:02 +0000</pubDate>
      <link>https://dev.to/amrguaily/hash-tables-full-guide-h2f</link>
      <guid>https://dev.to/amrguaily/hash-tables-full-guide-h2f</guid>
      <description>&lt;p&gt;Hash Table is a data structure that maps keys to values. You can then retrieve a certain value by using its key.&lt;/p&gt;

&lt;h4&gt;
  
  
  The way Hash Table Work?
&lt;/h4&gt;

&lt;p&gt;• It takes a &lt;strong&gt;Key&lt;/strong&gt; and then runs it through a &lt;strong&gt;Hash Function&lt;/strong&gt;.&lt;br&gt;
• A Hash Function maps strings to numbers and these numbers &lt;br&gt;
  correspond to indices in an Array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F2rb5uux1col6s4ifdoh6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2rb5uux1col6s4ifdoh6.png" alt="hash function" width="572" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to implement a Hash Table in JavaScript?
&lt;/h3&gt;

&lt;p&gt;We will talk about &lt;strong&gt;3&lt;/strong&gt; ways to implement a Hash Table in JS:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Using &lt;code&gt;Object&lt;/code&gt; data type.&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Using a &lt;code&gt;Map&lt;/code&gt; Object.&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Implement your own Hash Table.&lt;/p&gt;




&lt;h2&gt;
  
  
  1️⃣Using &lt;code&gt;Object&lt;/code&gt; data type - simplest implementation
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F40s1qe9hlewokkd2p1a1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F40s1qe9hlewokkd2p1a1.png" alt="Object as HashTable" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;u&gt;There are some downsides to this approach:&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; The &lt;code&gt;Object&lt;/code&gt; comes with its own properties and methods. The problem here is JS doesn't block an attempt to overwrite these methods, which may cause an error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzu34h95ux3s8afauroow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzu34h95ux3s8afauroow.png" alt="code1" width="800" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; The keys are limited to &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;Symbol&lt;/code&gt; data type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; The size of Object is not tracked, you need to manually determine it.&lt;br&gt;
&lt;a href="https://media2.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%2F1g3gkmak9jqg200evvxm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1g3gkmak9jqg200evvxm.png" alt="determine object length" width="717" height="270"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  2️⃣Using &lt;code&gt;Map&lt;/code&gt; object
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Map&lt;/code&gt; object is created to implement Hash Table without downsides that appeared on basic &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;u&gt;Unlike Object:&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;• &lt;code&gt;Map&lt;/code&gt;have &lt;code&gt;size&lt;/code&gt;property to track its contents.&lt;br&gt;
• Keys can be any data type.&lt;br&gt;
• You can't overwrite built-in properties/methods.&lt;br&gt;
• You must use the &lt;code&gt;set()&lt;/code&gt; and &lt;code&gt;get()&lt;/code&gt; methods to define and retrieve data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdo9t4ijgz1nv9wtaw8dz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdo9t4ijgz1nv9wtaw8dz.png" alt="Map as HasTable" width="667" height="232"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3️⃣Implement your own Hash Table
&lt;/h2&gt;

&lt;p&gt;You'll probably never have to implement Hash Table yourself, But writing your own Hash Table implementation is one of the most common JS interview questions.&lt;br&gt;
&lt;a href="https://media2.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%2Fi7vuss7y58zz8onnzzdd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fi7vuss7y58zz8onnzzdd.png" alt="Write your own HashTable" width="800" height="1478"&gt;&lt;/a&gt;&lt;br&gt;
Alright, all method work as expected. Let's try another insertion:&lt;br&gt;
&lt;a href="https://media2.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%2F2aja539dl99mzxe1b31w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2aja539dl99mzxe1b31w.png" alt="another insertion" width="606" height="207"&gt;&lt;/a&gt;&lt;br&gt;
😨!! When I call &lt;code&gt;table.get('name');&lt;/code&gt; it should retrieve &lt;code&gt;['name', 'Amr']&lt;/code&gt;, &lt;strong&gt;&lt;u&gt;So why is it not working as expected!?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;• &lt;code&gt;hash()&lt;/code&gt; function uses &lt;strong&gt;ASCII&lt;/strong&gt; code to convert key to an index. So if the keys are the same set of letters &lt;u&gt;(name == mane)&lt;/u&gt; the index will be the same. And this will cause overwriting the previous value with the new one.&lt;/p&gt;

&lt;p&gt;• This is called &lt;strong&gt;Collision&lt;/strong&gt; and needs to be handled in our implementation.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Handle Collision?
&lt;/h2&gt;

&lt;p&gt;Collision occurs when hash() function generate the same index for different keys.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;u&gt;There are 2 main ways to handle collision:&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Separate Chaining&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Open Addressing&lt;/p&gt;

&lt;p&gt;Here we'll just learn Separate Chaining..&lt;/p&gt;

&lt;h2&gt;
  
  
  ⏩ Handle Collision - Separate Chaining
&lt;/h2&gt;

&lt;p&gt;If multiple keys map to the same index, start a new &lt;code&gt;Array&lt;/code&gt; at this index.&lt;br&gt;
&lt;a href="https://media2.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%2Fo8nk4bdkc284k8v7prh8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fo8nk4bdkc284k8v7prh8.png" alt="Separate Chaining" width="622" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, Let's improve our &lt;code&gt;set()&lt;/code&gt; method to avoid collision:&lt;br&gt;
&lt;a href="https://media2.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%2Fvuauzyo9sogn6f4ihiet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvuauzyo9sogn6f4ihiet.png" alt="set method" width="770" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, update get() method&lt;br&gt;
&lt;a href="https://media2.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%2Fecd261xg5g67f6gf1jso.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fecd261xg5g67f6gf1jso.png" alt="get methpd" width="622" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and remove() method:&lt;br&gt;
&lt;a href="https://media2.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%2Fhra2aho2rpw22qxfj0np.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhra2aho2rpw22qxfj0np.png" alt="remove method" width="743" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, as a bonus let's add a display() method:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://media2.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%2F6oqbpf0gjejp6tt649kq.png" alt="display method" width="800" height="132"&gt;
&lt;/h2&gt;

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

&lt;p&gt;You have learned what Hash Table is and different ways to implement it in JavaScript.&lt;/p&gt;

&lt;p&gt;You've also learned how to implement your own Hash Table class as well as how to Handle Collisions.&lt;/p&gt;

&lt;p&gt;You'll commonly use a Hash Table because of its fast search, insertion, and delete operations:&lt;br&gt;
&lt;a href="https://media2.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%2Fvlc7a1v67nv6f7gv8dmf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvlc7a1v67nv6f7gv8dmf.png" alt="time complexity" width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Main Resources
&lt;/h3&gt;

&lt;p&gt;• Grokking Algorithms Book&lt;br&gt;
• &lt;a href="https://www.codecademy.com/resources/docs/javascript/hashtables" rel="noopener noreferrer"&gt;https://www.codecademy.com/resources/docs/javascript/hashtables&lt;/a&gt;&lt;br&gt;
• &lt;a href="https://www.youtube.com/watch?v=y3CcHKEM8r8" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=y3CcHKEM8r8&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
