<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Luan Carlos Macedo de Almeida</title>
    <description>The latest articles on Forem by Luan Carlos Macedo de Almeida (@luan_carlosmacedodealm).</description>
    <link>https://forem.com/luan_carlosmacedodealm</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%2F1626144%2Fe7a23d30-d300-4e2e-b3bc-28b0fc9e18d8.png</url>
      <title>Forem: Luan Carlos Macedo de Almeida</title>
      <link>https://forem.com/luan_carlosmacedodealm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/luan_carlosmacedodealm"/>
    <language>en</language>
    <item>
      <title>You don't need React.memo</title>
      <dc:creator>Luan Carlos Macedo de Almeida</dc:creator>
      <pubDate>Mon, 26 Aug 2024 15:45:20 +0000</pubDate>
      <link>https://forem.com/luan_carlosmacedodealm/you-dont-need-reactmemo-274p</link>
      <guid>https://forem.com/luan_carlosmacedodealm/you-dont-need-reactmemo-274p</guid>
      <description>&lt;p&gt;Many developers use React.memo without understanding why a component is rendering and its usage, with the pressure to meet deadlines and deliver the committed tickets we often find ourselves just using it because “it’s working”.&lt;/p&gt;

&lt;p&gt;Before we walk through React.memo let’s start this post with a simple question 🙋🏽&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do you know why your component is re-reading?&lt;/strong&gt;&lt;br&gt;
I’m not here to provide a deep explanation, so here’s the straightforward answer: Your component has been re-rendered because its state has been updated ✨🦄✨.&lt;/p&gt;

&lt;p&gt;So, if I’m telling you that you don’t need React.memo, then how do you fix your re-rendering issues? 😡&lt;/p&gt;

&lt;p&gt;Image the scenario where you have an App rendering 2 other children, but only one of them needs to receive the updated value.&lt;/p&gt;

&lt;p&gt;In our example the Parent component has its state updated, and all the children will be re-rendered, like in the image below. Normally you fix this rendering issue by wrapping the children with “React.memo” or using another memorisation solution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeg8jrw4lcphfocq48rh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeg8jrw4lcphfocq48rh.png" alt="Entire app rendering diagram" width="800" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But I am here to show you two simple solutions for that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
Move the state manipulation to the child component witch need it, isolating the render and creating a better separation of concerns.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1wxscfxl7uwnafki5vr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1wxscfxl7uwnafki5vr.png" alt="First solution" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
Make the Parent element accept a child, it means the child won’t be re-rendered when the state of the parent change, using that approach you can create a optimised and flexible component&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bg85myq9dyhf2znbzqe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bg85myq9dyhf2znbzqe.png" alt="Second solution" width="800" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am not here to advocate against React memorisation! Instead I just want to show you that you don’t need to use diferentes features or tools to improve your performance, &lt;strong&gt;you just need to organize your code better.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is all folks! &lt;/p&gt;

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