<?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: Wesam Alqawasmeh</title>
    <description>The latest articles on DEV Community by Wesam Alqawasmeh (@wesamalqawasmeh).</description>
    <link>https://dev.to/wesamalqawasmeh</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%2F803223%2F376db7f2-1c12-4b81-b345-3bbbb869650b.png</url>
      <title>DEV Community: Wesam Alqawasmeh</title>
      <link>https://dev.to/wesamalqawasmeh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wesamalqawasmeh"/>
    <language>en</language>
    <item>
      <title>React useCallback and JS function closure 🤔</title>
      <dc:creator>Wesam Alqawasmeh</dc:creator>
      <pubDate>Tue, 21 Feb 2023 12:07:12 +0000</pubDate>
      <link>https://dev.to/wesamalqawasmeh/react-usecallback-and-js-function-closure-2h8c</link>
      <guid>https://dev.to/wesamalqawasmeh/react-usecallback-and-js-function-closure-2h8c</guid>
      <description>

&lt;p&gt;As many of you know that &lt;code&gt;useCallback&lt;/code&gt; is a &lt;strong&gt;&lt;em&gt;built-in hook in React&lt;/em&gt;&lt;/strong&gt;, usually used to scale and optimize large applications rendering performance by memoizing the functions to reduce recreating them in each re-render occurs. But you may not know how this hook actually works and how that is related to function closures, so this is what I'm gonna explain here!&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ If you don't know why &lt;code&gt;useCallback&lt;/code&gt; is used for, I recommend you to go through &lt;a href="https://beta.reactjs.org/reference/react/useCallback" rel="noopener noreferrer"&gt;useCallback in React docs&lt;/a&gt; before you keep going in this blog.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;useCallback&lt;/code&gt; takes two parameters, a callback function and&lt;br&gt;
an array of dependencies. In the first render &lt;code&gt;useCallback&lt;/code&gt; returns the passed function, and for subsequent renders it compares the old dependencies with the passed one using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is" rel="noopener noreferrer"&gt;Object.is&lt;/a&gt; algorithm, If nothing has been changed in the dependencies will return the same old function, otherwise will return the passed function.&lt;/p&gt;

&lt;p&gt;🔻See this simple skeleton for &lt;code&gt;useCallback&lt;/code&gt;🔻&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoizedCallback = useCallback(
  () =&amp;gt; {
    // function logic goes here
  },
  [dependencyArray]
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔻And this is a simulation for &lt;code&gt;useCallback&lt;/code&gt; as a normal JS function🔻&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*
    Please note that this code is theoretical, this is not 
    how we actually compare two arrays together.
 */

let oldDependancies, oldCallBack; //React stores these somewhere

function useCallback(callback, dependencies) {
  if(oldDependancies === dependencies) return oldCallBack;

  oldDependancies = dependencies;
  oldCallBack = callback;

  return callback;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Theoretically&lt;/em&gt;&lt;/strong&gt; the above code compares the old dependencies with the passed one, if the old one is the same as the passed one then will return the old callback function, otherwise it will assign the new dependencies array and the new callback to the old one, and returns the passed function.&lt;/p&gt;

&lt;p&gt;Now lets dive into with a real example, the great &lt;strong&gt;&lt;em&gt;Counter component&lt;/em&gt;&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;import React, { useState, useCallback } from "react";

function Counter() {
  const [count, setCount] = useState(0);

  const incrementCount = useCallback(() =&amp;gt; {
    setCount(count + 1);
  }, [count]);

  const decrementCount = useCallback(() =&amp;gt; {
    setCount(count - 1);
  }, [count]);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={incrementCount}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={decrementCount}&amp;gt;Decrement&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component renders a count number with increment and decrement buttons. Each of increment and decrement handlers memoized (cached) using &lt;code&gt;useCallback&lt;/code&gt;, and it's working as expected when click the buttons will increase and decrease the count by &lt;code&gt;1&lt;/code&gt;. You can interact with it using the below box!&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/hsgq1z"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Let's change the above code a bit by removing the &lt;code&gt;count&lt;/code&gt; from the dependencies array in each &lt;code&gt;useCallback&lt;/code&gt; for &lt;code&gt;incrementCount&lt;/code&gt; and &lt;code&gt;decrementCount&lt;/code&gt;, to be like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const incrementCount = useCallback(() =&amp;gt; {
    setCount(count + 1);
  }, []);

  const decrementCount = useCallback(() =&amp;gt; {
    setCount(count - 1);
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you guess now what are the results when click &lt;code&gt;increment&lt;/code&gt; and &lt;code&gt;decrement&lt;/code&gt; buttons multiple times? check your answer using the below box&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/sd5tdg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Now after you've clicked many times on the buttons, apparently the count won't increase more than 1 or decrease less than -1, and the reason behind that is the function &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#closure" rel="noopener noreferrer"&gt;closure&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;useCallback&lt;/code&gt; with an empty dependency array tills react to create the &lt;code&gt;incrementCount&lt;/code&gt; and &lt;code&gt;decrementCount&lt;/code&gt; functions only once, and not to recreate them on every render. Therefore, the closure of these functions contains the initial (stale) value of count, which is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;incrementCount&lt;/code&gt; function is called, it sets the value of &lt;code&gt;count&lt;/code&gt; to &lt;code&gt;count + 1&lt;/code&gt;, but count is still the old value of &lt;code&gt;0&lt;/code&gt; in this closure, so the result is &lt;code&gt;1&lt;/code&gt;. On subsequent calls to &lt;code&gt;incrementCount&lt;/code&gt;, the closure still contains the old value of &lt;code&gt;count&lt;/code&gt;, which is &lt;code&gt;0&lt;/code&gt;, so the result is still &lt;code&gt;1&lt;/code&gt;. And the same for &lt;code&gt;decrementCount&lt;/code&gt; the result will allways be &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The solution for this issue is to add each variable affects the result to the dependency array, &lt;strong&gt;or&lt;/strong&gt; by passing an &lt;a href="https://beta.reactjs.org/reference/react/useState#updating-state-based-on-the-previous-state" rel="noopener noreferrer"&gt;updater function&lt;/a&gt; to &lt;code&gt;setCount&lt;/code&gt; instead of &lt;code&gt;count +- 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;incrementCount&lt;/code&gt; will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const incrementCount = useCallback(() =&amp;gt; {
    setCount(count + 1);
  }, [count]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const incrementCount = useCallback(() =&amp;gt; {
    setCount((prevCount) =&amp;gt; prevCount + 1);
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason is that updater function works with an empty dependency array, is this updater function is called during the rendering process and reads the actual value of the state at that time, to know more check &lt;a href="https://beta.reactjs.org/learn/queueing-a-series-of-state-updates" rel="noopener noreferrer"&gt;queueing a series of state updates&lt;/a&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Each variable affects the result of the function passed to &lt;code&gt;useCallback&lt;/code&gt; should be added to the dependency array.&lt;/li&gt;
&lt;li&gt;If the state change depends on the old state it's better pass an updater function to the state setter function.&lt;/li&gt;
&lt;li&gt;The memoized callback will has the same old closure until re-creating it because of dependency changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Thanks for reading!&lt;/p&gt;

&lt;p&gt;Let me know if you have any thoughts by commenting below.&lt;/p&gt;

</description>
      <category>puzzlegames</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
