<?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>Forem: Esraa Ismail</title>
    <description>The latest articles on Forem by Esraa Ismail (@esraaismail).</description>
    <link>https://forem.com/esraaismail</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%2F880002%2F4e6d720e-a154-466c-baa2-4fece2836a87.jpeg</url>
      <title>Forem: Esraa Ismail</title>
      <link>https://forem.com/esraaismail</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/esraaismail"/>
    <language>en</language>
    <item>
      <title>How to create custom Hooks in React.Js?</title>
      <dc:creator>Esraa Ismail</dc:creator>
      <pubDate>Sun, 11 Dec 2022 14:17:51 +0000</pubDate>
      <link>https://forem.com/esraaismail/how-to-create-custom-hooks-in-reactjs-5ej7</link>
      <guid>https://forem.com/esraaismail/how-to-create-custom-hooks-in-reactjs-5ej7</guid>
      <description>&lt;p&gt;React hooks are a new feature published by react team in version 16.8, it's allow you to use the state and other react features.&lt;br&gt;
In simple terms hooks are functions that connect react state and lifecycle features from the function components, don't forget react hooks only work in react functions.&lt;br&gt;
Besides built-in Hooks such as( useState, useEffect, useCallback…), you can build your own hooks.&lt;/p&gt;

&lt;h3&gt;
  
  
  So what are custom hooks?🧐
&lt;/h3&gt;

&lt;p&gt;Custom hooks are normal javaScript function that accepts arguments and return outputs but they contain stateful logic.&lt;br&gt;
They are like other hooks so their name starts with 'use' and can be used to call other hooks, we use it in react function and we call it only in the top level of the component&lt;/p&gt;

&lt;h3&gt;
  
  
  Why and When to use React Custom Hooks?
&lt;/h3&gt;

&lt;p&gt;We use it any time when we need to share a logic between different components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom hooks give us some benefits:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reusability&lt;/strong&gt;: we can use the same hook again and again, without the need to write it twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability&lt;/strong&gt;: separate logic from the component to a custom hook will make the component easier to read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean-code&lt;/strong&gt;: Fix logic in one place (custom hooks) becomes easier than changing it in more than one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;let's see an example make simple API call using Axios &lt;/p&gt;

&lt;p&gt;components/AllPosts.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cqv_EWfv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9n1ssjd0d3aor4wpd86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cqv_EWfv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9n1ssjd0d3aor4wpd86.png" alt="components/AllPosts.js" width="880" height="859"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We send GET request using axios without any headers or body to fetch posts with response as result or error.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;components/AddPost.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dsLxL5oF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in5oou4wttqwilnq5jtr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dsLxL5oF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in5oou4wttqwilnq5jtr.png" alt="components/AddPost.js" width="880" height="922"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here we send POST request with headers and body of the post using axios.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Look at the previous examples, you can see that both components above have a very similar logic. They all call API ,The only difference is that they render different UI , different URL when calling API and different request method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can reduce the repetition by creating a custom hook for example  &lt;em&gt;useAxios()&lt;/em&gt; for reuse as follows:&lt;/p&gt;

&lt;p&gt;src/hooks/useAxios.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z5pDK1Fl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fs50tulwjghu748oadqf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z5pDK1Fl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fs50tulwjghu748oadqf.png" alt="src/hooks/useAxios.js" width="880" height="794"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here we made a reusable hook can accept an argument that contain the request details like (URL, request method, headers and body)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So let's modify our components &lt;/p&gt;

&lt;p&gt;components/AllPosts.js &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g7ioqh4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjkdlkfsdosucf460by8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g7ioqh4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjkdlkfsdosucf460by8.png" alt="components/AllPosts.js" width="880" height="753"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;components/AddPost.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KChBAZzd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51i4bvqtejnrkba89avp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KChBAZzd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51i4bvqtejnrkba89avp.png" alt="components/AddPost.js" width="880" height="686"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
we have learned the importance of using custom hooks like saving time, make your code clean reusable and readable, also we have  shown real examples from project with an explanation of when we used certain hooks.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>customhooks</category>
    </item>
    <item>
      <title>How JavaScript work behind the scene</title>
      <dc:creator>Esraa Ismail</dc:creator>
      <pubDate>Tue, 21 Jun 2022 00:24:43 +0000</pubDate>
      <link>https://forem.com/esraaismail/how-javascript-work-behind-the-scene-3on3</link>
      <guid>https://forem.com/esraaismail/how-javascript-work-behind-the-scene-3on3</guid>
      <description>&lt;blockquote&gt;
&lt;h2&gt;
  
  
  Did you ask yourself before how JavaScript code is executed?🤔
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's go in-depth to understand how &lt;/p&gt;

&lt;p&gt;We all know that JavaScript is a single-threaded and non-blocking scripting language ,what is that mean ? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Single-threaded&lt;/strong&gt;: which means that only one thing can happen at a time&lt;br&gt;
&lt;strong&gt;Non-blocking&lt;/strong&gt; : imagine you’re running a function which takes 30 seconds during that task we’re waiting for 30 seconds before anything else can happen !! Of course, that doesn't happen&lt;br&gt;&lt;br&gt;
It means that it doesn't wait for the response of an asynchronous code&lt;/p&gt;

&lt;p&gt;To better understand asynchronous JavaScript, let’s look at the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Goodbye!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    

&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello 2 &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Think with me what is the final result ..&lt;/p&gt;
&lt;/blockquote&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%2Fuploads%2Farticles%2Fo3q0t1c7vhvg5646a59c.jpeg" 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%2Fuploads%2Farticles%2Fo3q0t1c7vhvg5646a59c.jpeg" alt="code result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;what happen in the above code ?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;JavaScript run line by line but it is non blocking language so it will run ready functions and functions take time execute later &lt;br&gt;
So with asynchronous JavaScript, the JavaScript doesn’t wait for responses when executing a function, so it executes 'Hello 1' at first then 'Hello 2' and after 3 seconds it execute 'Goodbye!'&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%2Fuploads%2Farticles%2F5iw460fnparwf0hyedx1.png" 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%2Fuploads%2Farticles%2F5iw460fnparwf0hyedx1.png" alt="JavaScript execution "&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The actual JavaScript execution is really simple, but it ties to four concepts you need to understand first.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Call stack&lt;/li&gt;
&lt;li&gt;Web APIs&lt;/li&gt;
&lt;li&gt;Callback Queue&lt;/li&gt;
&lt;li&gt;Event loop&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Call stack&lt;/strong&gt; :&lt;br&gt;
   JavaScript has only one call stack, The call stack is part of &lt;br&gt;
   the JS engine, When we run code we invoke a function, it gets &lt;br&gt;
   added to the call stack. If this function is asynchronous like &lt;br&gt;
   (timeout(setTimeOut),Ajax request, ..) it moves to web Apis . &lt;br&gt;
   This means that only one thing can be executed at a time&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Web Apis&lt;/strong&gt; :&lt;br&gt;
   It is a place where asynchronous functions exist till it &lt;br&gt;
   have finished execution, after finishing It simple gets added &lt;br&gt;
   to the Callback queue&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callback Queue&lt;/strong&gt;:&lt;br&gt;
   When the browser is done with the time (or any other API which &lt;br&gt;
   it provides for JS), it doesn't transfer the codes to be &lt;br&gt;
   executed back to JavaScript immediately. When the browser is &lt;br&gt;
   done, it stores the codes in a callback queue. It is a queue &lt;br&gt;
   containing some functions or codes which would be called back &lt;br&gt;
   at a later time.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event loop&lt;/strong&gt;:&lt;br&gt;
   has one simple job, it checks if the main stack is empty, and &lt;br&gt;
   when it is empty, it checks the Callback Queue. If there are &lt;br&gt;
   codes in the queue to be executed, they are transferred one by &lt;br&gt;
   one to the call stack. After the code is executed, it leaves &lt;br&gt;
   the stack and the next one in the queue comes up until the &lt;br&gt;
   queue is empty&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%2Fuploads%2Farticles%2Fc4ux5jlavfmhpuikum8l.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%2Fuploads%2Farticles%2Fc4ux5jlavfmhpuikum8l.gif" alt="stack "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;above gif explain the idea &lt;br&gt;
 we can see that the first function is pushed to the call stack &lt;br&gt;
 and Hi is immediately executed in the console, then asynchronous &lt;br&gt;
 function (setTimeOut) transfer from call stack to web apis till &lt;br&gt;
 it finish execution then move to callback queue , when call stack &lt;br&gt;
 is empty event loop transfer asynchronous function to stack to &lt;br&gt;
 execute .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
  we learned who synchronous and asynchronous code is execute &lt;br&gt;
  behind the scene , and we know what is the meaning of call &lt;br&gt;
  stack, web apis, callback queue and event loop .&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>eventloop</category>
    </item>
  </channel>
</rss>
