<?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 Adel</title>
    <description>The latest articles on Forem by Mohamed Adel (@moadel).</description>
    <link>https://forem.com/moadel</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%2F668359%2F08362361-c1a0-4fe6-98ee-221ea848149a.png</url>
      <title>Forem: Mohamed Adel</title>
      <link>https://forem.com/moadel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/moadel"/>
    <language>en</language>
    <item>
      <title>React optimization: useMemo() , React.memo() &amp; useCallback()</title>
      <dc:creator>Mohamed Adel</dc:creator>
      <pubDate>Wed, 19 Oct 2022 11:45:08 +0000</pubDate>
      <link>https://forem.com/moadel/usememo-reactmemo-usecallback-4c49</link>
      <guid>https://forem.com/moadel/usememo-reactmemo-usecallback-4c49</guid>
      <description>&lt;h1&gt;
  
  
  Memo &amp;amp; useCallback
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Best performance and optimization training&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Before optimization&lt;/em&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Making enviroment with the issue of all components re-rendering&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understanding how react works and re-evaluation : re-running everything again in react except states declaring.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Difference between re-run/re-evaluate &amp;amp; re-render&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;em&gt;&lt;strong&gt;re-evaluate&lt;/strong&gt;&lt;/em&gt; should work every time and compare&lt;/li&gt;
&lt;li&gt; &lt;em&gt;&lt;strong&gt;re-render&lt;/strong&gt;&lt;/em&gt; works on change only with calling react-dom&lt;/li&gt;
&lt;li&gt; All react components will re-rerun (not re-render) everytime if we didn't use any optimization&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;&lt;strong&gt;Optimization methods&lt;/strong&gt;&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;It's a trial to make re-run/re-evaluate works like re-render on changes only&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React.memo()&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;it saves the component states and props to get checked first before the re-run&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;It caches the props and states of the components so using it alot in useless way will make it caching a lot so it will be heavy on runtime.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//some javascript&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="c1"&gt;//some jsx&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;useMemo()&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;difference between reference types in Js&lt;br&gt;
  &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;


&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

 &lt;span class="c1"&gt;//name === name2 --&amp;gt; true&lt;/span&gt;

 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="c1"&gt;//obj === obj2 --&amp;gt; false&lt;/span&gt;
 &lt;span class="c1"&gt;//objects on different refrences values&lt;/span&gt;

 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;
 &lt;span class="c1"&gt;//passing by refrence&lt;/span&gt;

 &lt;span class="c1"&gt;//obj === obj3 --&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;ul&gt;
&lt;li&gt;Primitive type&lt;/li&gt;
&lt;li&gt;Refrence type : Object, array &amp;amp; function
&lt;p&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;useMemo is used with arrays, objects &amp;amp; with functions that returns something to fix the problem in re-decleration with every re-reun&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adel&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;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useMemo prevents object re-declaration with new refrence on re-run&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;useCallback()&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;useCallback() prevents function (with no return) from re-declaration with new refrence on re-rerun
 &lt;p&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="c1"&gt;//this won't work it will be re-declared with new refrence every re-run&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//function logic with no return&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;//solution&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//function logic&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="nx"&gt;addAge&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;addAge&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;useCallback caches the logic itself not a return value like useMemo&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>TS or JS to be upcoming</title>
      <dc:creator>Mohamed Adel</dc:creator>
      <pubDate>Mon, 24 Jan 2022 08:56:07 +0000</pubDate>
      <link>https://forem.com/moadel/ts-or-js-to-be-upcoming-2d6c</link>
      <guid>https://forem.com/moadel/ts-or-js-to-be-upcoming-2d6c</guid>
      <description></description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Quick tip : How to stick your footer down in your page whatever its size?!</title>
      <dc:creator>Mohamed Adel</dc:creator>
      <pubDate>Wed, 04 Aug 2021 22:48:10 +0000</pubDate>
      <link>https://forem.com/moadel/quick-tip-how-to-stick-your-footer-down-in-your-page-whatever-its-size-28bj</link>
      <guid>https://forem.com/moadel/quick-tip-how-to-stick-your-footer-down-in-your-page-whatever-its-size-28bj</guid>
      <description>&lt;p&gt;`1- in css give the parent class which wrap the whole page lets say container display flex with direction column with min-height 100vh&lt;br&gt;
2- go to footer class for css and give it margin-top auto&lt;/p&gt;

&lt;h1&gt;
  
  
  example:
&lt;/h1&gt;

&lt;p&gt;.container {&lt;br&gt;
  min-height: 100vh;&lt;br&gt;
  display: flex;&lt;br&gt;
  flex-direction: column;&lt;br&gt;
}&lt;br&gt;
.footer{&lt;br&gt;
margin-top: auto;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;I hope it helps you!!`&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
