<?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: Mohamed Ahmed Elhalwagui</title>
    <description>The latest articles on Forem by Mohamed Ahmed Elhalwagui (@elhalwaguiahmedmohamed).</description>
    <link>https://forem.com/elhalwaguiahmedmohamed</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%2F861179%2F74f51500-fda9-4186-a950-aea74648a665.jpeg</url>
      <title>Forem: Mohamed Ahmed Elhalwagui</title>
      <link>https://forem.com/elhalwaguiahmedmohamed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/elhalwaguiahmedmohamed"/>
    <language>en</language>
    <item>
      <title>useEffect hook from the prespective of a noob</title>
      <dc:creator>Mohamed Ahmed Elhalwagui</dc:creator>
      <pubDate>Fri, 13 May 2022 18:58:42 +0000</pubDate>
      <link>https://forem.com/elhalwaguiahmedmohamed/useeffect-hook-from-the-prespective-of-a-noob-k77</link>
      <guid>https://forem.com/elhalwaguiahmedmohamed/useeffect-hook-from-the-prespective-of-a-noob-k77</guid>
      <description>&lt;p&gt;&lt;em&gt;Have you ever wondered why this hook is called useEffect ?&lt;/em&gt;&lt;br&gt;
Simply because it manages the side-effects of a component, But what are side-effects?&lt;/p&gt;
&lt;h2&gt;
  
  
  Side-effects
&lt;/h2&gt;

&lt;p&gt;side-effects can be any thing that does not target the output value of the function for examples: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Asynchronous api calls to fetch data&lt;/li&gt;
&lt;li&gt;Setting a subscribtion to an observable&lt;/li&gt;
&lt;li&gt;Manually updating the dom&lt;/li&gt;
&lt;li&gt;Updating global variables from inside a function
Code example :
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {react} from 'react'
function Greet({name}){
 const message = "Hello, ${name}";
 document.title = message; // this is a side-effect

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

&lt;/div&gt;

&lt;h2&gt;
  
  
  Arguments
&lt;/h2&gt;

&lt;p&gt;the useEffect hook accepts two arguments &lt;br&gt;
&lt;code&gt;useEffect(callback function , [dependencies])&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The callback function is to execute the side-effects you want to happen after the render.&lt;/li&gt;
&lt;li&gt;The array of dependencies is to tell the useEffect hook when to execute the callback function that performs the side-effects.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The dependencies array
&lt;/h2&gt;

&lt;p&gt;the dependencies array argument can be in three forms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not provided =&amp;gt; means that the callback function will be executed after each render and that may cause an infinite loop
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {react} from 'react'
function Greet({name}){
 const message = "Hello, ${name}";

 useEffect(()=&amp;gt;{
   document.title = message;
 })
 return &amp;lt;div&amp;gt;{message}&amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Provided as an empty array =&amp;gt; means that the callback function will be executed after the initial render only [Mounting]
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {react} from 'react'
function Greet({name}){
 const message = "Hello, ${name}";

 useEffect(()=&amp;gt;{
   document.title = message;
 },[])
 return &amp;lt;div&amp;gt;{message}&amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Has dependencies =&amp;gt; usually the dependencies are props and state and the callback function will be called after the initial render and after the change of any dependency
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {react} from 'react'
function Greet({name}){
 const message = "Hello, ${name}";

 useEffect(()=&amp;gt;{
   document.title = message;
 },[name])
 return &amp;lt;div&amp;gt;{message}&amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Clean up
&lt;/h2&gt;

&lt;p&gt;The useEffect hook always expects to return nothing or to return a function this function is used for clean up. Some side-effects need a clean up like to clean up a timer or to close up a socket connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {react} from 'react'
function Greet({name}){
 const message = "Hello, ${name}";

 const timer = useEffect(()=&amp;gt;{
   setInterval(()=&amp;gt;{
      document.title = message;
   },2000)

   return function cleanUp(){
      clearInterval(timer);
   }
 },[name])
 return &amp;lt;div&amp;gt;{message}&amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final Note&lt;/strong&gt;:&lt;br&gt;
&lt;em&gt;The callback function cannot be async because async functions return a promise and the useEffect hook always expects the callback function wheather to return nothing or to return a clean up function&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More resources:
&lt;/h2&gt;

&lt;p&gt;Dimitri Pavlutin article =&amp;gt; &lt;a href="https://dmitripavlutin.com/react-useeffect-explanation/"&gt;Link&lt;/a&gt;&lt;br&gt;
Ben Awad youtube video =&amp;gt; &lt;a href="https://www.youtube.com/watch?v=j1ZRyw7OtZs&amp;amp;t=23s"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you arrived to this point I hope you enjoied the article and learned somthing new ^^ .&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
