<?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: Alex Sidorenko</title>
    <description>The latest articles on DEV Community by Alex Sidorenko (@sidkh).</description>
    <link>https://dev.to/sidkh</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%2F369175%2F998c3baa-3806-4106-b8cc-a7f17bc39b6a.jpg</url>
      <title>DEV Community: Alex Sidorenko</title>
      <link>https://dev.to/sidkh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sidkh"/>
    <language>en</language>
    <item>
      <title>A Visual Guide to useEffect - Cleanups</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 21 Dec 2021 09:04:08 +0000</pubDate>
      <link>https://dev.to/sidkh/a-visual-guide-to-useeffect-cleanups-13mf</link>
      <guid>https://dev.to/sidkh/a-visual-guide-to-useeffect-cleanups-13mf</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a second chapter of the &lt;a href="https://alexsidorenko.com/blog/useeffect/" rel="noopener noreferrer"&gt;Visual Guide to useEffect&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here is a component that logs how many seconds have passed from the first time it’s rendered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdm1n49m8wlrneys6mhl2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdm1n49m8wlrneys6mhl2.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Effects may keep running after the component unmounts
&lt;/h2&gt;

&lt;p&gt;Now let’s say we have two pages in our app. Only one of those pages has our component. Here is what happens when we switch between the pages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dmokj2v057kbbnd4hnr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dmokj2v057kbbnd4hnr.gif" alt="Component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The log keeps running after the component unmounts. How to prevent that?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every effect may return a function that cleans up after it&lt;/p&gt;

&lt;p&gt;React performs the cleanup when the component unmounts&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;React Docs - useEffect&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpibp44vz1oyvurdy6dw2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpibp44vz1oyvurdy6dw2.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Effects may accumulate with every execution
&lt;/h2&gt;

&lt;p&gt;Let’s change our component to log how many seconds have passed from the &lt;strong&gt;last time&lt;/strong&gt; it is rendered. We will need to remove the dependency array to run the effect after every re-render.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj80vjleai82d4gtvcx6t.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj80vjleai82d4gtvcx6t.gif" alt="Effects may accumulate"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we keep re-rendering the component, we will keep creating new intervals. How do we only keep the last interval? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React &lt;em&gt;also&lt;/em&gt; cleans up effects from the previous render before running the effects next time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;React Docs - useEffect&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwzxx24tntnd98xn88su.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwzxx24tntnd98xn88su.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  When to use cleanups?
&lt;/h2&gt;

&lt;p&gt;Whenever you add an effect, think about what will happen when the component unmounts. Will this effect keep running? If so, provide a cleanup function. If you do that, it will automatically cover the second use case when effects accumulate with every execution.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This effect won't keep running after the component unmounts.&lt;/em&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Doesn't require a cleanup&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This effect will keep running after the component unmounts.&lt;/em&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Requires a cleanup&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handler&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pageYOffset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onScroll&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;/div&gt;






&lt;p&gt;Looking for daily snippets of visual React content? &lt;a href="https://twitter.com/asidorenko_" rel="noopener noreferrer"&gt;Follow me on Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A 2 Minute Visual Guide to useEffect</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 07 Dec 2021 09:11:54 +0000</pubDate>
      <link>https://dev.to/sidkh/a-2-minute-visual-guide-to-useeffect-46c0</link>
      <guid>https://dev.to/sidkh/a-2-minute-visual-guide-to-useeffect-46c0</guid>
      <description>&lt;p&gt;Here is what happens when you try to fetch data directly from the body of a functional component in React 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uwTtfSQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8f8vb7ueifgwuri5cnf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uwTtfSQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8f8vb7ueifgwuri5cnf.gif" alt="Image description" width="880" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why does this happen and what tools does React offer to solve this problem?&lt;/p&gt;




&lt;h2&gt;
  
  
  Side effects
&lt;/h2&gt;

&lt;p&gt;If your React component affects anything outside of itself, it's called a side effect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6RPhUxCG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/195vlozltczbysnz504x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6RPhUxCG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/195vlozltczbysnz504x.png" alt="Image description" width="880" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Side effects shouldn't happen during component render. Therefore they do not belong to the body of a functional component. React has a special place for them. &lt;/p&gt;

&lt;p&gt;To learn more about side effects and why they need a special treatment, check out this brilliant section in the new React documentation - &lt;a rel="noreferrer" href="https://beta.reactjs.org/learn/keeping-components-pure"&gt;Keeping Components Pure&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dealing with side effects
&lt;/h2&gt;

&lt;p&gt;The special place for side effects is inside the &lt;code&gt;useEffect&lt;/code&gt; hook. Hence the name.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we’ll refer to it as our “effect”), and call it later after performing the DOM updates.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-effect.html#example-using-hooks"&gt;React Docs - Using the Effect Hook&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's try it with our initial component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9MQID7mo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wita221zf6u0191duxkc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9MQID7mo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wita221zf6u0191duxkc.gif" alt="Image description" width="880" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; does not run during the render. It runs after the render, which is what we want. But we still have the same problem. That's because, by default, &lt;code&gt;useEffect&lt;/code&gt; will run after every component render.&lt;/p&gt;




&lt;h2&gt;
  
  
  Running effects conditionally
&lt;/h2&gt;

&lt;p&gt;There is a way to run effects conditionally, not after every render. The &lt;code&gt;useEffect&lt;/code&gt; hook accepts the dependency list as a second argument. React will only re-run the effect if any dependency in the list changes. Let's try it out:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5dpR77yn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di3t6tfj0xh1cgo03vfq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5dpR77yn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di3t6tfj0xh1cgo03vfq.gif" alt="Image description" width="880" height="573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you pass an empty array as a dependency list, the effect will run only once, after the first render. It prevents the infinite loop from happening in our case. However, you might notice that our fetch function relies on the &lt;code&gt;props.id&lt;/code&gt; .&lt;/p&gt;




&lt;h2&gt;
  
  
  Reacting to changes
&lt;/h2&gt;

&lt;p&gt;Let's pick up where we left off and see what happens when we change the component's props.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ung3zbiB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogdfb4wu5gnhxh58wiy2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ung3zbiB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogdfb4wu5gnhxh58wiy2.gif" alt="Image description" width="880" height="607"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We would expect the component to react to prop change and fetch another product, but nothing happens. That's because we have an empty dependency list. &lt;/p&gt;

&lt;p&gt;If the side effect relies on any props or state variables, we should put them in the dependency list. After every render, React will check whether any dependency has changed, and if it did, will re-run the effect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vafG9e5A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s1ms7xnja70yim1twp6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vafG9e5A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s1ms7xnja70yim1twp6.gif" alt="Image description" width="880" height="607"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  To be continued...
&lt;/h2&gt;

&lt;p&gt;There are a couple more things to learn about useEffect. Don't miss the next 2 minute guide. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://alexsidorenko.com/"&gt;https://alexsidorenko.com/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Visual Guide to React Rendering - Refs</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 16 Nov 2021 11:01:48 +0000</pubDate>
      <link>https://dev.to/sidkh/a-visual-guide-to-react-rendering-refs-56mb</link>
      <guid>https://dev.to/sidkh/a-visual-guide-to-react-rendering-refs-56mb</guid>
      <description>&lt;p&gt;How to disable this button after 3 clicks without re-rendering the component first 2 times? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe2gw3h6089b51p6usgyw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe2gw3h6089b51p6usgyw.gif" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Button&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Storing values with refs
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;When you want a component to “remember” some information, but you don’t want that information to &lt;a href="https://beta.reactjs.org/learn/render-and-commit" rel="noopener noreferrer"&gt;trigger new renders&lt;/a&gt;, you can use a &lt;strong&gt;ref&lt;/strong&gt;—it’s like a secret “pocket” for storing information in your component!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://beta.reactjs.org/learn/referencing-values-with-refs" rel="noopener noreferrer"&gt;React Docs - Referencing Values with Refs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's try to count clicks with refs instead of state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtochfrsqr7wlv23yygr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvtochfrsqr7wlv23yygr.gif" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Button&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ref updates, but the button is still active. Why is that?&lt;/p&gt;




&lt;h2&gt;
  
  
  Component renders and DOM updates
&lt;/h2&gt;

&lt;p&gt;To disable the button react has to update the DOM. React updates the DOM when the component renders with a different output than before. React won’t update any DOM until one of the React components render. And since changing ref doesn’t cause the component to re-render, the button stays active.&lt;/p&gt;

&lt;p&gt;To demonstrate this point further, let's add a parent component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxj76wb15zjp9jn9lfd1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxj76wb15zjp9jn9lfd1.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default when you render a React component it recursively re-renders all its children. That's why when we update the &lt;code&gt;Parent&lt;/code&gt; state, it renders both &lt;code&gt;Parent&lt;/code&gt; and &lt;code&gt;Component&lt;/code&gt;. And when &lt;code&gt;Component&lt;/code&gt; renders, React executes the condition and disables a button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Button&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But surely we can not rely on parent updates to disable a button. Let's implement this behavior directly into &lt;code&gt;Component&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Update state to re-render a component
&lt;/h2&gt;

&lt;p&gt;We can make the component re-render by reintroducing state again. But we still don't want the component to render on first 2 clicks. So we will keep the ref to silently count the clicks. And we will add a state variable with a sole responsibility of updating the button status. Let's update &lt;code&gt;butttonStatus&lt;/code&gt; only when the button clicked the third time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
  &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;buttonStatus&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;disabled&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setButtonStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;disabled&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;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedssj9qdveneyy5ootj3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedssj9qdveneyy5ootj3.gif" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The example from the article has been made to demonstrate the behavior of refs. Keep in mind that unnecessary renders are not always bad, and you don't have to eliminate every one of them. In fact, in a real-world scenario, it would probably make more sense to rely solely on the state and re-render this component 3 times for simplicity. However, you will encounter different situations in your apps. Understanding refs gives you a powerful tool for fine-tuning the behavior of your components. &lt;/p&gt;




&lt;p&gt;For daily snippets of React content, follow me on &lt;a href="https://twitter.com/asidorenko_" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/alex-sidorenko-32b435b3/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How Code Splitting Improves Lighthouse Score</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 02 Nov 2021 10:45:24 +0000</pubDate>
      <link>https://dev.to/sidkh/how-code-splitting-improves-lighthouse-score-1gb4</link>
      <guid>https://dev.to/sidkh/how-code-splitting-improves-lighthouse-score-1gb4</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T1gX75Ve--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iegsfvbiwt0iq5rxe4lu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T1gX75Ve--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iegsfvbiwt0iq5rxe4lu.png" alt="Low Lighthouse performance score" width="880" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if there was one single technique that could help increase this score by a lot?&lt;/p&gt;




&lt;h2&gt;
  
  
  Why having a large JS bundle is bad for performance
&lt;/h2&gt;

&lt;p&gt;Let's say we have a &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/SPA"&gt;single-page application&lt;/a&gt; with six screens. Every screen requires a particular amount of javascript to work. But since it is a single-page application, we load all javascript in one bundle and include it in our &lt;code&gt;index.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--glYMCWR4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/842rcvvzlmd9y15q0q0s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--glYMCWR4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/842rcvvzlmd9y15q0q0s.png" alt="Six screens - Home, About, Dashboard, Admin, Post, Posts" width="880" height="704"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, when a user lands on Home, ideally, we only want to load 100kb of javascript required for this screen. But instead, the browser has to load and parse the entire javascript bundle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9dCJj3mp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0mwogqa2wcz3rs9w9hl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9dCJj3mp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0mwogqa2wcz3rs9w9hl.png" alt="Home screen downloads an entire javascript bundle" width="880" height="704"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even if the user never accesses "Admin" or "Dashboard" pages, the code for these pages will still have to be loaded. Moreover, if we don't use pre-rendering techniques, the browser will not show the Home screen until the entire bundle is loaded. Therefore bundle loading process blocks the rendering and increases the time for the &lt;a href="https://web.dev/first-contentful-paint/"&gt;First Contentful Paint&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  How code-splitting helps
&lt;/h2&gt;

&lt;p&gt;What if we could load the code for each screen only when the user lands on this screen. Let's say the user goes from &lt;em&gt;Home&lt;/em&gt; to &lt;em&gt;Posts&lt;/em&gt; to a single &lt;em&gt;Post&lt;/em&gt;. In this case, we don't want to load javascript for &lt;em&gt;Admin&lt;/em&gt;, &lt;em&gt;Dashboard&lt;/em&gt; and &lt;em&gt;About&lt;/em&gt; at all. How do we do that?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XBiAzG-8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0t6il77as3g5qtg4gu3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XBiAzG-8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0t6il77as3g5qtg4gu3.gif" alt="Loading screens" width="880" height="704"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is where code splitting comes in handy. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Code splitting is the splitting of code into various bundles or components which can then be loaded on demand or in parallel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Code_splitting"&gt;MDN - Code splitting&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of having one large javascript bundle, we split it into smaller chunks and load them on demand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cUitLNPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qo592a5z9gr4l4effzdi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cUitLNPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qo592a5z9gr4l4effzdi.gif" alt="Splitting code into smaller chunks" width="880" height="727"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a user lands on &lt;em&gt;Home&lt;/em&gt;, the browser loads 100kb &lt;code&gt;home.js&lt;/code&gt; and immediately starts rendering the &lt;em&gt;Home&lt;/em&gt; screen. If then, the user goes to &lt;em&gt;Posts&lt;/em&gt;, the browser loads 200kb &lt;code&gt;posts.js&lt;/code&gt; and renders &lt;em&gt;Posts&lt;/em&gt; screen, etc. No more unused code, no render-blocking.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to apply code splitting
&lt;/h2&gt;

&lt;p&gt;There are different code splitting techniques available for various javascript bundlers and frameworks: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/code-splitting.html"&gt;Code Splitting - React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://web.dev/route-level-code-splitting-in-angular/"&gt;Route-level code splitting in Angular&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://vueschool.io/articles/vuejs-tutorials/lazy-loading-and-code-splitting-in-vue-js/"&gt;Lazy loading and code splitting in Vue.js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://webpack.js.org/guides/code-splitting/"&gt;Code Splitting - Webpack&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Follow me on &lt;a href="https://twitter.com/asidorenko_"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
    <item>
      <title>A Visual Guide to React Rendering - Cheat Sheet</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 19 Oct 2021 09:05:11 +0000</pubDate>
      <link>https://dev.to/sidkh/a-visual-guide-to-react-rendering-cheat-sheet-2482</link>
      <guid>https://dev.to/sidkh/a-visual-guide-to-react-rendering-cheat-sheet-2482</guid>
      <description>&lt;p&gt;When does react component re-renders? What can cause the re-render, and how to prevent unnecessary renders? Here is a quick cheat sheet you can refer to whenever you find yourself asking these questions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article serves as a table of content for a &lt;a href="/blog/react-render-always-rerenders/"&gt;Visual Guide to React Rendering&lt;/a&gt; series. Every section of the cheat sheet links to the correspondent chapter of the guide that explores a topic in depth.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Standard rendering and memo
&lt;/h2&gt;

&lt;p&gt;By default, when the state of the component changes, this component and &lt;strong&gt;all its children&lt;/strong&gt; re-render. You can wrap React component with &lt;code&gt;memo&lt;/code&gt; to prevent an entire subtree from re-rendering.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb8gtqj690khv8vbcfk0e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb8gtqj690khv8vbcfk0e.gif" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://alexsidorenko.com/blog/react-render-always-rerenders/" rel="noopener noreferrer"&gt;A Visual Guide To React Rendering - It Always Rerenders (Chapter 1)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Primitive vs Non-primitive props
&lt;/h2&gt;

&lt;p&gt;Non-primitive values in javascript are compared by reference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;flex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;flex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep that in mind when passing props to memoized components. Memoized components re-render when their props change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvb1ettwkfzxqgd9jlv0v.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvb1ettwkfzxqgd9jlv0v.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://alexsidorenko.com/blog/react-render-props/" rel="noopener noreferrer"&gt;A Visual Guide To React Rendering - Props (Chapter 2)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Stable reference with useMemo
&lt;/h2&gt;

&lt;p&gt;You can preserve a reference to a non primitive value with &lt;code&gt;useMemo&lt;/code&gt;. It won't change on re-renders.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F819blf5o4qfxghq0ykpx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F819blf5o4qfxghq0ykpx.gif" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://alexsidorenko.com/blog/react-render-usememo/" rel="noopener noreferrer"&gt;A Visual Guide To React Rendering - useMemo (Chapter 3)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Stable reference with useCallback
&lt;/h2&gt;

&lt;p&gt;You can preserve a reference to a function with &lt;code&gt;useCallback&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7gebd4w1q7j8884hpu2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7gebd4w1q7j8884hpu2.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://alexsidorenko.com/blog/react-render-usecallback/" rel="noopener noreferrer"&gt;A Visual Guide To React Rendering - useCallback (Chapter 4)&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Rendering and Context
&lt;/h2&gt;

&lt;p&gt;The component right under your context provider should probably use &lt;code&gt;memo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmd83elstst9smak3gsg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmd83elstst9smak3gsg.gif" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://alexsidorenko.com/blog/react-render-context/" rel="noopener noreferrer"&gt;A Visual Guide To React Rendering - Context (Chapter 5)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Rendering and DOM
&lt;/h2&gt;

&lt;p&gt;React component render does not mean DOM update. React is smart enough to update only those parts of DOM that need to change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffst9efyaqhxs0o3g5qtu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffst9efyaqhxs0o3g5qtu.gif" alt="Image description"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://alexsidorenko.com/blog/react-render-dom/" rel="noopener noreferrer"&gt;A Visual Guide To React Rendering - DOM (Chapter 6)&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://alexsidorenko.com/blog/react-render-cheat-sheet/" rel="noopener noreferrer"&gt;alexsidorenko.com&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Articles on the topic:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/" rel="noopener noreferrer"&gt;A (Mostly) Complete Guide to React Rendering Behavior&lt;/a&gt; - Mark Erikson&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://kentcdodds.com/blog/fix-the-slow-render-before-you-fix-the-re-render" rel="noopener noreferrer"&gt;Fix the slow render before you fix the re-render&lt;/a&gt; - Kent C. Dodds&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://overreacted.io/before-you-memo/" rel="noopener noreferrer"&gt;Before You memo()&lt;/a&gt; - Dan Abramov&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Detect Slow Renders in React?</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Wed, 06 Oct 2021 11:19:05 +0000</pubDate>
      <link>https://dev.to/sidkh/how-to-detect-slow-renders-in-react-5a0b</link>
      <guid>https://dev.to/sidkh/how-to-detect-slow-renders-in-react-5a0b</guid>
      <description>&lt;p&gt;Improving React app performance often comes down to finding bottlenecks and fixing them. One well-placed memoization can make a slow app fast again. But how to find performance bottlenecks? &lt;/p&gt;




&lt;h2&gt;
  
  
  Profile the problem
&lt;/h2&gt;

&lt;p&gt;Open &lt;a href="https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en" rel="noopener noreferrer"&gt;Rect Developer Tools&lt;/a&gt; Profiler tab. Click the record button to start profiling. Interact with the part of your app that feels slow, then click the record button again to stop profiling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftzerdjf2vt4crepokyuy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftzerdjf2vt4crepokyuy.gif" alt="profile"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h2&gt;
  
  
  Analyze the results
&lt;/h2&gt;

&lt;p&gt;Find a slow commit you want to improve. You can see the commits bar in the right top corner of profiling results. For more info on commits, check out &lt;a href="https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html#browsing-commits" rel="noopener noreferrer"&gt;React Docs - Browsing Commits&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vomerelylltev9cul3w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vomerelylltev9cul3w.gif" alt="commits"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our case, 1st, 2nd, and 4th commits are slow. They take more than 300ms to render. Every response to a user action that takes more than 100ms breaks the connection between the action and the result (&lt;a href="https://www.smashingmagazine.com/2015/10/rail-user-centric-model-performance/#putting-the-user-in-the-center-of-performance" rel="noopener noreferrer"&gt;RAIL: A User-Centric Model For Performance&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Now let's pick one of these commits and check the "Flamegraph" to see what is causing this poor performance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fng9ktriw0oha8pvouzqk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fng9ktriw0oha8pvouzqk.gif" alt="flamegraph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Flamegraph shows our components tree. We can see that component &lt;code&gt;Home&lt;/code&gt; and its entire sub-tree re-renders. The &lt;code&gt;SearchResults&lt;/code&gt; component responsible for the main UI change is pretty fast and takes only 7.4ms to render. The &lt;code&gt;SlowComponent&lt;/code&gt; takes most of the rendering time. It is the bottleneck.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fix the bottleneck
&lt;/h2&gt;

&lt;p&gt;Let's look into the code of a &lt;code&gt;SlowComponent&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SlowComponent&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;// Expensive calculation that takes 300+ms&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()].&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Expensive calculation - &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;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;/div&gt;



&lt;p&gt;We can wrap our expensive calculation with &lt;code&gt;useMemo&lt;/code&gt; to make sure it only runs when necessary. And since we don't rely on any of the props, we can leave the dependency array empty. This way, our expensive calculation will not be re-triggered every time &lt;code&gt;SlowComponent&lt;/code&gt; re-renders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SlowComponent&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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="c1"&gt;// Expensive calculation that takes 300+ms&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()].&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Expensive calculation - &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;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;/div&gt;



&lt;p&gt;Now let's analyze performance again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgxno7zm7w466bgc6g80.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgxno7zm7w466bgc6g80.gif" alt="profile-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The UI feels faster already. Let's check the commits.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvz2cu2ign33c0wwq89z4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvz2cu2ign33c0wwq89z4.gif" alt="commits-2"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The 1st, 2nd, and 4th commits are still the slowest. But they each take around 12-17ms to render, which is 14 times faster than before. Let's analyze the Flamegraph to see what happened.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe1y5nm6b1fi0srf8512w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe1y5nm6b1fi0srf8512w.gif" alt="flamegraph-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;SearchResults&lt;/code&gt; component takes the most time to render now. But since it's only 12ms, we have nothing to worry about. And now that we put our memoization in place, the &lt;code&gt;SlowComponent&lt;/code&gt; takes only 0.3ms to render.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://alexsidorenko.com/blog/react-performance-slow-renders/" rel="noopener noreferrer"&gt;alexsidorenko.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>performance</category>
    </item>
    <item>
      <title>My React App is Slow and I Don't Know Why</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 28 Sep 2021 09:59:20 +0000</pubDate>
      <link>https://dev.to/sidkh/my-react-app-is-slow-and-i-don-t-know-why-1c7p</link>
      <guid>https://dev.to/sidkh/my-react-app-is-slow-and-i-don-t-know-why-1c7p</guid>
      <description>&lt;p&gt;What performance testing tools are available in React world, and how to start using them?&lt;/p&gt;




&lt;h2&gt;
  
  
  Inspect what components are getting re-rendered
&lt;/h2&gt;

&lt;p&gt;You can use &lt;a href="https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en" rel="noopener noreferrer"&gt;React Developer Tools&lt;/a&gt; to get a quick overview of what components re-render. Enable option &lt;em&gt;"Highlight updates when components render"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dom6xgypvcdiu8cu9oz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dom6xgypvcdiu8cu9oz.gif" alt="rerender"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Figure out what renders to fix
&lt;/h2&gt;

&lt;p&gt;Not all renders are equal. Some are less performant than others. Don't try to fix every unnecessary render. Instead, find bottlenecks and fix them. &lt;a href="https://www.notion.so/My-React-App-is-Slow-and-I-Don-t-Know-Why-f8b67e4b37c44d23b685b66d1f545c08" rel="noopener noreferrer"&gt;React Developer Tools&lt;/a&gt; Profiler tab will help you with this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb43p71jjmacpai3keik2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb43p71jjmacpai3keik2.gif" alt="react-profiler"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=00RoZflFE34" rel="noopener noreferrer"&gt;Ben Award - How to use the React Profiler to find and fix Performance Problems&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Investigate other performance problems
&lt;/h2&gt;

&lt;p&gt;Often, web app performance issues are not related to React. Analytics tracking libraries, excessive CSS animations, non-optimized images, iframes, and many more factors can contribute to poor performance. You can use the Chrome DevTools Performance tab to debug these issues.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexfgn1q6qk0toin3atus.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexfgn1q6qk0toin3atus.gif" alt="chrome-profiler"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.chrome.com/docs/devtools/evaluate-performance/#analyze" rel="noopener noreferrer"&gt;Chrome Dev Tools documentation - Analyze runtime performance.&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://alexsidorenko.com/blog/react-performance-tools/" rel="noopener noreferrer"&gt;alexsidorenko.com&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React is Declarative - What Does it Mean?</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 21 Sep 2021 11:39:08 +0000</pubDate>
      <link>https://dev.to/sidkh/react-is-declarative-what-does-it-mean-5d14</link>
      <guid>https://dev.to/sidkh/react-is-declarative-what-does-it-mean-5d14</guid>
      <description>&lt;p&gt;It's drilled into you when learning React that it is &lt;em&gt;declarative&lt;/em&gt;. But what does &lt;em&gt;declarative&lt;/em&gt; mean, and what is the opposite of &lt;em&gt;declarative&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Declarative vs Imperative UI
&lt;/h2&gt;

&lt;p&gt;The opposite of &lt;em&gt;declarative&lt;/em&gt; is &lt;em&gt;imperative&lt;/em&gt;. These are not React-specific terms. They apply to programming in general.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/1784664/what-is-the-difference-between-declarative-and-imperative-paradigm-in-programmin"&gt;StackOverflow - What is the difference between declarative and imperative paradigm in programming?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In terms of web user interfaces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The imperative approach is when you provide step-by-step DOM mutations until you reach desired UI&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The declarative approach is when you describe the final state of the desired UI&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example of declarative vs imperative UI
&lt;/h2&gt;

&lt;p&gt;Let's say we need to build this user interface:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q92DInYk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3999u1bm812ahziuavii.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q92DInYk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3999u1bm812ahziuavii.gif" alt="example1"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Imperative approach
&lt;/h3&gt;

&lt;p&gt;Here is the imperative solution. For every interaction, we provide step-by-step DOM mutations to reach the desired state of UI. &lt;em&gt;(The example is slowed down to show how each instruction affects the user interface)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T2xC3fuI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsmtm96pezlabk4sc0dn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T2xC3fuI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsmtm96pezlabk4sc0dn.gif" alt="example-imperative"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Declarative approach
&lt;/h3&gt;

&lt;p&gt;Here is a declarative React solution. We don't provide step-by-step instructions to reach the desired UI. Instead, we describe the final UI we want for each scene. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R96SHpVy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v26gfs4by3iwyscpr4n9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R96SHpVy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v26gfs4by3iwyscpr4n9.gif" alt="example-declarative"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We may structure the component differently. It doesn't make it less declarative.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SAqR22wJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwfaigwqgwbujfftg1aj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SAqR22wJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwfaigwqgwbujfftg1aj.gif" alt="example-declarative-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What makes it declarative is that we describe a final UI for any given state representation. It is the opposite of providing manual DOM mutations to transition between UI states, which would be considered imperative.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://alexsidorenko.com/blog/react-is-declarative-what-does-it-mean/"&gt;alexsidorenko.com&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Visual Guide to React Rendering - It Always Re-renders</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Wed, 21 Jul 2021 14:48:00 +0000</pubDate>
      <link>https://dev.to/sidkh/a-visual-guide-to-react-rendering-it-always-re-renders-1j8p</link>
      <guid>https://dev.to/sidkh/a-visual-guide-to-react-rendering-it-always-re-renders-1j8p</guid>
      <description>&lt;p&gt;When does a React component re-render? Is it when its state or props change?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hjPvqaGm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32y5r2g89bmlj4egrxr9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hjPvqaGm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32y5r2g89bmlj4egrxr9.gif" alt="React components re-render in response to parent render"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a look at the gif above 👆&lt;/p&gt;

&lt;p&gt;The structure of the app is: &lt;code&gt;App &amp;gt; A &amp;gt; B &amp;gt; C&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is a slightly simplified code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&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;// state&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ComponentA&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ComponentA&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ComponentB&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ComponentB&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ComponentC&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Components &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;B&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; don't have any props or state. Yet, they still re-render when the App renders.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In normal rendering, React does not care whether "props changed" - it will render child components unconditionally just because the parent rendered!&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Mark Erikson - &lt;a href="https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#standard-render-behavior"&gt;A (Mostly) Complete Guide to React Rendering Behavior&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To illustrate this point further, let's add a state to every component and track the behavior.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ePPa2Z0y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y31e7inaj1swjltuy3q0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ePPa2Z0y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y31e7inaj1swjltuy3q0.gif" alt="The tree of react components. Each has a button that updates its state."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the state of &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; changes, only &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; renders. But when the state of &lt;strong&gt;&lt;em&gt;B&lt;/em&gt;&lt;/strong&gt; changes, both &lt;strong&gt;&lt;em&gt;B&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; render. The &lt;strong&gt;&lt;em&gt;B&lt;/em&gt;&lt;/strong&gt; renders because its state updates, and &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; renders because its parent renders.&lt;/p&gt;

&lt;p&gt;When the state of &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; changes, &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; renders because of the state update, &lt;strong&gt;&lt;em&gt;B&lt;/em&gt;&lt;/strong&gt; renders because &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt; rendered, and &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; renders because &lt;strong&gt;&lt;em&gt;B&lt;/em&gt;&lt;/strong&gt; rendered.&lt;/p&gt;




&lt;h2&gt;
  
  
  Avoiding re-renders
&lt;/h2&gt;

&lt;p&gt;There are several ways to avoid unnecessary re-renders in React. In this article, I will only focus on &lt;code&gt;React.memo&lt;/code&gt; and save other methods for future posts. If you are interested in alternatives to &lt;code&gt;memo&lt;/code&gt; check out Dan Abramov's &lt;a href="https://overreacted.io/before-you-memo/"&gt;Before you memo()&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;
Also please keep in mind that in this post, I only explore re-renders caused by direct state update, or parent render. I don't pass any props.
&lt;/p&gt;  

&lt;p&gt;If you wrap a component in &lt;code&gt;memo&lt;/code&gt;, it won't re-render when its parent renders.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1esMnRD3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1q6js6n0a7hqyu5d5pl9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1esMnRD3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1q6js6n0a7hqyu5d5pl9.gif" alt="The tree of react components. The last one wrapped in memo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; updates when its state changes, but not when the parent renders.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lifting memo up
&lt;/h3&gt;

&lt;p&gt;Let's lift &lt;code&gt;memo&lt;/code&gt; up and see what happens.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MEsBY6H3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/233jpr14v4e05o6k1g13.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MEsBY6H3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/233jpr14v4e05o6k1g13.gif" alt="The tree of react components. The first one wrapped in memo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The state update of components &lt;strong&gt;&lt;em&gt;A&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;B&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;C&lt;/em&gt;&lt;/strong&gt; produces the same results as before. But notice the state update on the App. &lt;strong&gt;Wrapping a component with &lt;code&gt;memo&lt;/code&gt; prevents the entire subtree of this component from re-rendering in response to parent render.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's why you can hear advice like this:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--WzU5w6Zp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1174863858332102656/SwClbJcA_normal.jpg" alt="Sophie Alpert profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Sophie Alpert
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @sophiebits
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ir1kO05j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      That React Component Right Under Your Context Provider Should Probably Use `React.memo`
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      07:22 AM - 16 Feb 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1228942768543686656" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFnoeFxk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1228942768543686656" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k6dcrOn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1228942768543686656" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRQc9lOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;h3&gt;
  
  
  What about adjacent components?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S8sZ1Osy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erje45721r7lmsnn2xl2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S8sZ1Osy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erje45721r7lmsnn2xl2.gif" alt="The tree of react components. One of the adjacent components wrapped in memo."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same rules apply to adjacent components. The component with &lt;code&gt;memo&lt;/code&gt; doesn't re-render in response to parent render, and therefore, prevents its entire subtree from re-rendering.&lt;/p&gt;




&lt;h2&gt;
  
  
  Should I memo everything?
&lt;/h2&gt;

&lt;p&gt;If &lt;code&gt;memo&lt;/code&gt; has such a great effect on performance, does it make sense to wrap everything with it? Well, not really. But this is a topic for another day. If you are interested, read &lt;a href="https://kentcdodds.com/blog/fix-the-slow-render-before-you-fix-the-re-render"&gt;Fix the slow render before you fix the re-render&lt;/a&gt; by Kent C. Dodds.&lt;/p&gt;




&lt;h2&gt;
  
  
  To be continued...
&lt;/h2&gt;

&lt;p&gt;There is more to the rendering mechanism of React. To make the article simple, I purposefully left out things like passing props, context, DOM updates, and additional techniques to avoid re-rendering. I will write a separate post on each of these topics.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://alexsidorenko.com/blog/react-render-always-rerenders/"&gt;alexsidorenko.com&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to stop re-rendering lists in React?</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Wed, 14 Jul 2021 08:14:00 +0000</pubDate>
      <link>https://dev.to/sidkh/how-to-stop-re-rendering-lists-in-react-5gl</link>
      <guid>https://dev.to/sidkh/how-to-stop-re-rendering-lists-in-react-5gl</guid>
      <description>&lt;p&gt;You have a list of components in React. The parent holds the state and passes it to the list items. Every time you update the property of one of the components in the list, the entire list re-renders. How to prevent that?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tjk7gkqol1g0oqunp3o.gif" class="article-body-image-wrapper"&gt;&lt;img alt="List of react components. Every component in the list re-renders, even though the property updates for a single component." src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tjk7gkqol1g0oqunp3o.gif"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Components always re-render
&lt;/h2&gt;

&lt;p&gt;First, let's simplify our example by removing all props from the &lt;code&gt;Item&lt;/code&gt;. We will still update the parent state but won't pass any props to list items.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzohv89wsxc743g6slx25.gif" class="article-body-image-wrapper"&gt;&lt;img alt="List of react components that rerenders when parent renders" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzohv89wsxc743g6slx25.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a common misconception that a React component will not re-render unless one of its properties changes. This is not true:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React does not care whether "props changed" - it will render child components unconditionally just because the parent rendered!&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Mark Erikson - &lt;a href="https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#standard-render-behavior" rel="noopener noreferrer"&gt;A (Mostly) Complete Guide to React Rendering Behavior&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you don't want a component to re-render when its parent renders, wrap it with &lt;a href="https://reactjs.org/docs/react-api.html#reactmemo" rel="noopener noreferrer"&gt;memo&lt;/a&gt;. After that, the component indeed will only re-render when its props change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;memo&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Item&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqh67x2mzfv72hapxq8h6.gif" class="article-body-image-wrapper"&gt;&lt;img alt="List of react components that doesn't rerender when parent renders" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqh67x2mzfv72hapxq8h6.gif"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Applying memo to our problem
&lt;/h2&gt;

&lt;p&gt;Let's get back to our initial example and wrap &lt;code&gt;Item&lt;/code&gt; with &lt;code&gt;memo&lt;/code&gt;. Here is a slightly simplified code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onChange&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="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
      &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;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;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tjk7gkqol1g0oqunp3o.gif" class="article-body-image-wrapper"&gt;&lt;img alt="List of react components. Every component in the list re-renders, even though the property updates for a single component." src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tjk7gkqol1g0oqunp3o.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It doesn't work. We still have the same problem. But why?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If the component wrapped with &lt;code&gt;memo&lt;/code&gt; re-renders, it means that one of its properties changes&lt;/strong&gt;. Let's figure out which one.&lt;/p&gt;




&lt;h2&gt;
  
  
  Memoizing properties
&lt;/h2&gt;

&lt;p&gt;We know from looking at the state that &lt;code&gt;value&lt;/code&gt; only changes for one item in the list. The &lt;code&gt;id&lt;/code&gt; property is also stable. So it must be &lt;code&gt;onChange&lt;/code&gt; property that changes. Let's check the &lt;code&gt;Parent&lt;/code&gt; code to see how we pass the props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Parent&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setItems&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&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;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;
          &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
            &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;/div&gt;



&lt;p&gt;Here is our problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&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;/div&gt;



&lt;p&gt;&lt;strong&gt;Anonymous functions will always get a new reference on every render&lt;/strong&gt;. This means that &lt;code&gt;onChange&lt;/code&gt; property will change every time &lt;code&gt;Parent&lt;/code&gt; renders. To prevent that, we need to memoize it with &lt;a href="https://reactjs.org/docs/hooks-reference.html#usecallback" rel="noopener noreferrer"&gt;useCallback&lt;/a&gt;. Let's do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Parent&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="p"&gt;...&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="nf"&gt;setItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}))&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;
          &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tjk7gkqol1g0oqunp3o.gif" class="article-body-image-wrapper"&gt;&lt;img alt="List of react components. Every component in the list re-renders, even though the property updates for a single component." src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1tjk7gkqol1g0oqunp3o.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It still doesn't work - every component re-renders.&lt;/p&gt;

&lt;p&gt;This happens because we put &lt;code&gt;items&lt;/code&gt; as a dependency for &lt;code&gt;useCallback&lt;/code&gt; . Every time &lt;code&gt;items&lt;/code&gt; update, &lt;code&gt;useCallback&lt;/code&gt; returns a new reference of the function. This causes &lt;code&gt;onChange&lt;/code&gt; prop to change, therefore updating every component in the list.&lt;/p&gt;

&lt;p&gt;To fix this, we need to stop relying on &lt;code&gt;items&lt;/code&gt; as a dependency. We can achieve that with a &lt;a href="https://reactjs.org/docs/hooks-reference.html#functional-updates" rel="noopener noreferrer"&gt;functional state update&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="nf"&gt;setItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevItems&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}))&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="c1"&gt;// No dependencies&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpq2kwkzl9eirdqzro7kq.gif" class="article-body-image-wrapper"&gt;&lt;img alt="List of react components. Only one component updates when we update a property of this component" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpq2kwkzl9eirdqzro7kq.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the only property of the &lt;code&gt;Item&lt;/code&gt; that changes is &lt;code&gt;value&lt;/code&gt;. And since we only update one &lt;code&gt;value&lt;/code&gt; at a time, it prevents other components in the list from re-rendering.&lt;/p&gt;




&lt;h2&gt;
  
  
  Should I do that for every list?
&lt;/h2&gt;

&lt;p&gt;You don't have to optimize every unnecessary re-render in React. React render is quite performant. It only updates DOM when needed. And &lt;code&gt;memo&lt;/code&gt; comes with a small performance cost as well. Optimize it when you have a lot of items in the list and your render function is expensive.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I would assume that the same general advice applies for React.memo as it does for shouldComponentUpdate and PureComponent: doing comparisons does have a small cost, and there's scenarios where a component would never memoize properly (especially if it makes use of props.children). So, don't just automatically wrap everything everywhere. See how your app behaves in production mode, use React's profiling builds and the DevTools profiler to see where bottlenecks are, and strategically use these tools to optimize parts of the component tree that will actually benefit from these optimizations.&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Mark Erikson - &lt;a href="https://github.com/facebook/react/issues/14463#issuecomment-448829902" rel="noopener noreferrer"&gt;When should you NOT use React memo?&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://overreacted.io/before-you-memo/" rel="noopener noreferrer"&gt;Before you memo&lt;/a&gt; - Dan Abramov&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://kentcdodds.com/blog/fix-the-slow-render-before-you-fix-the-re-render" rel="noopener noreferrer"&gt;Fix the slow render before you fix the re-render&lt;/a&gt; - Kent C. Dodds&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Originally published at &lt;a href="https://alexsidorenko.com/blog/react-list-rerender/" rel="noopener noreferrer"&gt;alexsidorenko.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>3 ways to cause an infinite loop in React</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 06 Jul 2021 10:56:02 +0000</pubDate>
      <link>https://dev.to/sidkh/3-ways-to-cause-an-infinite-loop-in-react-55fk</link>
      <guid>https://dev.to/sidkh/3-ways-to-cause-an-infinite-loop-in-react-55fk</guid>
      <description>&lt;p&gt;Have you spent some time trying to debug an infinite loop in React? Maybe you hung your browser a couple of times in the process. Or had one of these 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught Error: Too many re-renders.
React limits the number of renders
to prevent an infinite loop.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are 3 potential causes of the infinite loop in React.&lt;/p&gt;




&lt;h2&gt;
  
  
  I. Updating the state inside the render
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// infinite loop&lt;/span&gt;

  &lt;span class="k"&gt;return&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;/div&gt;



&lt;p&gt;If you update the state directly inside your render method or a body of a functional component, it will cause an infinite loop.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;State updates → triggers re-render → state updates → triggers re-render → ...&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix  🎉
&lt;/h3&gt;

&lt;p&gt;Do you want to update a state only one time when the component mounts? Use &lt;code&gt;useEffect&lt;/code&gt; with an empty array as a dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&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="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  II. Infinite loop in useEffect
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// infinite loop&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

  &lt;span class="k"&gt;return&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;/div&gt;



&lt;p&gt;If you keep updating a state inside &lt;code&gt;useEffect&lt;/code&gt; with a property you update set as a dependency, it will cause an infinite loop.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;count&lt;/code&gt; updates → &lt;code&gt;useEffect&lt;/code&gt; detects updated dependency → &lt;code&gt;count&lt;/code&gt; updates → &lt;code&gt;useEffect&lt;/code&gt; detects updated dependency → ...&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix  🎉
&lt;/h3&gt;

&lt;p&gt;If you want to update a state based on its previous value, use a &lt;a href="https://reactjs.org/docs/hooks-reference.html#functional-updates"&gt;functional update&lt;/a&gt;. This way, you can remove state property from the dependency list and prevent an infinite loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;previousCount&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;previousCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  III. Incorrectly set event handlers
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// infinite loop&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;/div&gt;



&lt;p&gt;It is not the right way to set event handlers. You need to provide a &lt;strong&gt;function&lt;/strong&gt; to the &lt;code&gt;onClick&lt;/code&gt;, not &lt;strong&gt;the result of the function execution&lt;/strong&gt;. By executing a function before setting a handler, you update a state inside the render, which causes an infinite loop.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;State updates → triggers re-render → state updates → triggers re-render → ...&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix 🎉
&lt;/h3&gt;

&lt;p&gt;Set a &lt;strong&gt;function&lt;/strong&gt; to &lt;code&gt;onClick&lt;/code&gt; event. It is a proper way to set event handlers. This way state will only update after a click of a button and won't cause an infinite loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// infinite loop&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;/div&gt;






&lt;h2&gt;
  
  
  How to spot infinite loops
&lt;/h2&gt;

&lt;p&gt;Every time you update a state, picture the sequence of events that will happen after the update. If without additional user interaction, this sequence leads you back to the same state update, you probably have an infinite loop.&lt;/p&gt;




&lt;p&gt;Discuss on &lt;a href="https://mobile.twitter.com/asidorenko_/status/1412361231223099392"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Why is it so difficult to modify a deeply nested state in React?</title>
      <dc:creator>Alex Sidorenko</dc:creator>
      <pubDate>Tue, 29 Jun 2021 10:47:42 +0000</pubDate>
      <link>https://dev.to/sidkh/why-is-it-so-difficult-to-modify-a-deeply-nested-state-in-react-2lmh</link>
      <guid>https://dev.to/sidkh/why-is-it-so-difficult-to-modify-a-deeply-nested-state-in-react-2lmh</guid>
      <description>&lt;p&gt;It's not uncommon to see a code like that in React world. All this effort just to update a single to-do item inside a nested state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="nx"&gt;setProject&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&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;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;But how come that such a popular, widely spread tool as React makes such a hustle from something that a beginner may bump into during the first project? Is that really so that solo React can not tackle deeply nested state updates with less boilerplate? Isn't it a bit disappointing?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Immutability trade-off
&lt;/h2&gt;

&lt;p&gt;React embraces the concept of immutability. The above code example updates the state immutably. It means that it doesn't modify the state directly. Instead, it creates a new copy of the state. In comparison, this how a direct mutation of the state would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But why is immutability so important if it makes the trivial task of updating a state so complicated? Here are three main reasons from React documentation:&lt;/p&gt;

&lt;h3&gt;
  
  
  I
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Immutability makes complex features much easier to implement. Avoiding direct data mutation lets us keep previous versions of the state history intact, and reuse them later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  II
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Detecting changes in mutable objects is difficult because they are modified directly. This detection requires the mutable object to be compared to previous copies of itself and the entire object tree to be traversed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  III
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The main benefit of immutability is that it helps you build pure components in React. Immutable data can easily determine if changes have been made, which helps to determine when a component requires re-rendering.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;More information on immutability:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/tutorial/tutorial.html#why-immutability-is-important"&gt;React docs - Why Immutability Is Important&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://archive.jlongster.com/Using-Immutable-Data-Structures-in-JavaScript"&gt;Immutable data structures in Javascript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://daveceddia.com/why-not-modify-react-state-directly/"&gt;Why not to modify React state directly&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Ok, I get it - immutability is important, but how do I update my nested state?
&lt;/h2&gt;

&lt;p&gt;There are two main ways to simplify nested state updates in React - flattening the state and using immutable libraries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flattening the state
&lt;/h3&gt;

&lt;p&gt;The best solution is to avoid the problem. If you have a nested state, try to flatten it. Check out &lt;a href="https://stackoverflow.com/a/38856081/4882422"&gt;three main reasons to keep your state flat&lt;/a&gt; by the Redux maintainer, Mark Erikson. You can try to flatten your state by hand or use a third-party library, like &lt;a href="https://github.com/paularmstrong/normalizr"&gt;Normalirz&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using immutable libraries
&lt;/h3&gt;

&lt;p&gt;There are libraries out there that are designed to help with immutable updates. For example, here is how &lt;a href="https://immerjs.github.io/immer/"&gt;Immer&lt;/a&gt; can help to reduce our boilerplate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;produce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;baseState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;draftState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;draftState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;taskId&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todoId&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;draftState&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;/div&gt;



&lt;p&gt;Check out &lt;a href="https://openbase.com/categories/js/best-javascript-immutable-libraries"&gt;top 13 immutable libraries&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The pain of updating a nested state stems from the fundamental architectural decision of the React to embrace immutability. Immutability comes with plenty of great benefits, such as predictability and performance, so the trade-off is worth it.&lt;/p&gt;

&lt;p&gt;There are two main ways to deal with the problem of updating a deeply nested state. First is flattening your state to avoid the problem altogether. The second is using immutable libraries that help with state manipulations.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://alexsidorenko.com/blog/react-update-nested-state/"&gt;alexsidorenko.com&lt;/a&gt;&lt;/p&gt;

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