<?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: vsr</title>
    <description>The latest articles on Forem by vsr (@vsramalwan).</description>
    <link>https://forem.com/vsramalwan</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%2F360564%2Fab4e1e1b-92c8-4c3e-8c65-7cc39532a30f.jpeg</url>
      <title>Forem: vsr</title>
      <link>https://forem.com/vsramalwan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vsramalwan"/>
    <language>en</language>
    <item>
      <title>Ways to Handle Deep Object Comparison in useEffect hook</title>
      <dc:creator>vsr</dc:creator>
      <pubDate>Sun, 24 May 2020 17:47:06 +0000</pubDate>
      <link>https://forem.com/vsramalwan/ways-to-handle-deep-object-comparison-in-useeffect-hook-1elm</link>
      <guid>https://forem.com/vsramalwan/ways-to-handle-deep-object-comparison-in-useeffect-hook-1elm</guid>
      <description>&lt;p&gt;In React, side effects can be handled in functional components using &lt;code&gt;useEffect&lt;/code&gt; &lt;a href="https://reactjs.org/docs/hooks-effect.html"&gt;hook&lt;/a&gt;. In this post, I'm going to talk about the dependency array which holds our props/state and specifically what happens in case there's an object in this array.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook runs even if one element in the dependency array has changed. React does this for optimization purposes. On the other hand, if you pass an empty array then it never re-runs.&lt;/p&gt;

&lt;p&gt;However, things become complicated if an object is present in this array. Then even if the object is modified, the hook won't re-run because it doesn't do the deep object comparison between these dependency changes for the object. There are couple of ways to solve this problem.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Use lodash's &lt;code&gt;isEqual&lt;/code&gt; &lt;a href="https://lodash.com/docs/#isEqual"&gt;method&lt;/a&gt; and &lt;code&gt;usePrevious&lt;/code&gt; &lt;a href="https://reactjs.org/docs/hooks-faq.html"&gt;hook&lt;/a&gt;. This hook internally uses a ref object that holds a mutable &lt;code&gt;current&lt;/code&gt; property that can hold values.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s possible that in the future React will provide a usePrevious Hook out of the box since it’s relatively common use case.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const prevDeeplyNestedObject = usePrevious(deeplyNestedObject)
useEffect(()=&amp;gt;{
    if (
        !_.isEqual(
            prevDeeplyNestedObject,
            deeplyNestedObject,
        )
    ) {
        // ...execute your code
    }
},[deeplyNestedObject, prevDeeplyNestedObject])
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;useDeepCompareEffect&lt;/code&gt; &lt;a href="https://github.com/kentcdodds/use-deep-compare-effect"&gt;hook&lt;/a&gt; as a drop-in replacement for &lt;code&gt;useEffect&lt;/code&gt; hook for objects&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import useDeepCompareEffect from 'use-deep-compare-effect'
...
useDeepCompareEffect(()=&amp;gt;{
    // ...execute your code
}, [deeplyNestedObject])
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;useCustomCompareEffect&lt;/code&gt; &lt;a href="https://github.com/sanjagh/use-custom-compare-effect"&gt;hook&lt;/a&gt; which is similar to solution #2&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I've prepared a CodeSandbox &lt;a href="https://codesandbox.io/s/useeffect-deep-comparison-i5r92?file=/src/App.js"&gt;example&lt;/a&gt; related to this post. Fork it and check it yourself.&lt;/p&gt;

</description>
      <category>react</category>
      <category>useeffect</category>
      <category>deepcomparison</category>
      <category>hooks</category>
    </item>
  </channel>
</rss>
