<?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: Joaquin-Niembro</title>
    <description>The latest articles on DEV Community by Joaquin-Niembro (@joaquinniembro).</description>
    <link>https://dev.to/joaquinniembro</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%2F1230353%2F4279170e-0a32-4f29-9abc-1b9c6f3aad8d.jpeg</url>
      <title>DEV Community: Joaquin-Niembro</title>
      <link>https://dev.to/joaquinniembro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joaquinniembro"/>
    <language>en</language>
    <item>
      <title>Writing useEffect from scratch</title>
      <dc:creator>Joaquin-Niembro</dc:creator>
      <pubDate>Tue, 19 Dec 2023 18:58:35 +0000</pubDate>
      <link>https://dev.to/joaquinniembro/writing-useeffect-from-scratch-2gbm</link>
      <guid>https://dev.to/joaquinniembro/writing-useeffect-from-scratch-2gbm</guid>
      <description>&lt;p&gt;Polyfills are a good way to understand hooks better, even though they can seem challenging we should approach them as writing any custom hook, as the logic for writing any custom hook is the same for writing the hooks polypills for the most part!.&lt;/p&gt;

&lt;p&gt;We can start of with what the useEffect hook is, we can break it down to a function that receives 2 things, an &lt;strong&gt;Effect&lt;/strong&gt; which is nothing more that a function and a &lt;strong&gt;dependency array&lt;/strong&gt; (or not in case we want it to execute on every render.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCustomUseEffect(fn, deps) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can define how many functionalities and edge cases this function needs to be able to handle.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Execute the function on the first render.&lt;/li&gt;
&lt;li&gt;Find a way to know it is the first render.&lt;/li&gt;
&lt;li&gt;Cache the dependencies in order to compare after they have changed.&lt;/li&gt;
&lt;li&gt;Execute the effect again in case the dependencies have changed.&lt;/li&gt;
&lt;li&gt;Manage the cleanUp function and execute it after the effect.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Using refs for validating first render&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCustomUseEffect(fn, deps) {
    const firstTimeRef = useRef(true);

    if (firstTimeRef.current) {
        // execute code first time and then set it to false as it will revalidate each render
        firstTimeRef.current = false; 
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can go ahead and execute the effect on the first render, this way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCustomUseEffect(fn, deps) {
  const firstTimeRef = useRef(true);

  if (firstTimeRef.current) {
    firstTimeRef.current = false;
    fn();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to determine a way to verify the dependecies and if the dependencies change through renders, this makes a special case for refs once again as they persist through renders.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using refs to cache the dependency array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCustomUseEffect(fn, deps) {
  const firstTimeRef = useRef(true);
  const cacheDepsRef = useRef([]);

  if (firstTimeRef.current) {
    firstTimeRef.current = false;
    fn();
  }

  cacheDepsRef.current = deps ? deps : [];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we can compare the &lt;strong&gt;deps&lt;/strong&gt; that come from the argument with the &lt;strong&gt;cacheDepsRef.current&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;we can now move on to re-executing the effect every time the dependencies change, in order to do that we can compare the reps as mentioned above and trigger the effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execute effect when dependencies change&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCustomUseEffect(fn, deps) {
  const firstTimeRef = useRef(true);
  const cacheDepsRef = useRef([]);

  if (firstTimeRef.current) {
    firstTimeRef.current = false;
    fn();
  }
  // comparing previous deps with current deps
  if (JSON.stringify(deps) === JSON.stringify(cacheDepsRef.current)) {
    fn();
  }

  cacheDepsRef.current = deps ? deps : [];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we have only 2 more things left to do for our useEffect custom implementation.&lt;/p&gt;

&lt;p&gt;We are missing the case when the deps array does not exist and the clean up function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No dependencies are provided&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the case where we don't receive a dependency array we can simple add another validation to let it execute each render and we should be just fine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCustomUseEffect(fn, deps) {
  const firstTimeRef = useRef(true);
  const cacheDepsRef = useRef([]);

  if (firstTimeRef.current) {
    firstTimeRef.current = false;
    fn();
  }

  if (JSON.stringify(deps) === JSON.stringify(cacheDepsRef.current)) {
    fn();
  }

  // execute on every render
  if (!deps) {
    fn();
  }
  cacheDepsRef.current = deps ? deps : [];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally is time for cleaning up the effect!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clean up function implementation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useCustomUseEffect(fn, deps) {
  const firstTimeRef = useRef(true);
  const cacheDepsRef = useRef([]);

  if (firstTimeRef.current) {
    firstTimeRef.current = false;
    const cleanUp = fn();
    return () =&amp;gt; {
      if (cleanUp &amp;amp;&amp;amp; typeof cleanUp === "function") {
        cleanUp();
      }
    };
  }

  if (JSON.stringify(deps) === JSON.stringify(cacheDepsRef.current)) {
    const cleanUp = fn();
    return () =&amp;gt; {
      if (cleanUp &amp;amp;&amp;amp; typeof cleanUp === "function") {
        cleanUp();
      }
    };
  }

  if (!deps) {
    const cleanUp = fn();
    return () =&amp;gt; {
      if (cleanUp &amp;amp;&amp;amp; typeof cleanUp === "function") {
        cleanUp();
      }
    };
  }
  cacheDepsRef.current = deps ? deps : [];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As easy as that! ok, now all of our edge cases are completed!🚀&lt;/p&gt;

&lt;p&gt;So our custom useEffect is now completed! hopefully this is clear enough and anybody reading this is one step closer to being confident writing this custom implementations and adding more functionality on top!😃&lt;/p&gt;

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