<?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: Bruno Henrique</title>
    <description>The latest articles on DEV Community by Bruno Henrique (@brunohenrique00).</description>
    <link>https://dev.to/brunohenrique00</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%2F897299%2F2f561825-9630-4ed4-8bf0-eb4eda25a742.png</url>
      <title>DEV Community: Bruno Henrique</title>
      <link>https://dev.to/brunohenrique00</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brunohenrique00"/>
    <language>en</language>
    <item>
      <title>React, we need to talk about Derived States!</title>
      <dc:creator>Bruno Henrique</dc:creator>
      <pubDate>Tue, 10 Dec 2024 17:54:54 +0000</pubDate>
      <link>https://dev.to/brunohenrique00/react-we-need-to-talk-about-derived-states-3ljp</link>
      <guid>https://dev.to/brunohenrique00/react-we-need-to-talk-about-derived-states-3ljp</guid>
      <description>&lt;p&gt;When working with React, you’ll often encounter situations where you need to transform or derive values based on other state or props. This concept is known as &lt;strong&gt;&lt;em&gt;derived state&lt;/em&gt;&lt;/strong&gt;, and it's one of the most powerful tools in your React toolkit—if used correctly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Derived State: State that can be computed from existing piece of state or prop is called derived state.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, let’s dive into it and have a bit of fun while we’re at it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bad Practice: Filtering the Product List
&lt;/h2&gt;

&lt;p&gt;Let’s start with a typical "oops, I didn’t think this through" example. Imagine we have a list of products, and we want to filter them based on what the user types into a search input. In an ideal world, our search should update dynamically and be super snappy. But, let’s take a quick look at the &lt;strong&gt;bad practice&lt;/strong&gt; approach first.&lt;/p&gt;

&lt;p&gt;Here’s how we &lt;em&gt;shouldn’t&lt;/em&gt; be handling things:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&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;ProductList&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;searchQuery&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearchQuery&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="dl"&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;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setProducts&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;id&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Laptop&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Smartphone&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Headphones&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Smartwatch&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;filteredProducts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFilteredProducts&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="nx"&gt;products&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;handleSearch&lt;/span&gt; &lt;span class="o"&gt;=&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="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;query&lt;/span&gt; &lt;span class="o"&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="nf"&gt;setSearchQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setFilteredProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&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="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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&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;searchQuery&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;handleSearch&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Search for a product"&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&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;filteredProducts&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;product&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="nt"&gt;li&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;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&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;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;li&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;ul&lt;/span&gt;&lt;span class="p"&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;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ProductList&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;Why is this bad practice?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It looks like it works, right? The search input is hooked up, and it filters the products as expected.&lt;/p&gt;

&lt;p&gt;But here’s the problem: we’re storing the &lt;strong&gt;filtered products&lt;/strong&gt; as a separate state. This causes unnecessary duplication of data. We already have &lt;code&gt;products&lt;/code&gt; in state, and we’re storing the result of the filter operation in another state, which leads to potential bugs, increased memory usage, and makes it harder to keep everything in sync.&lt;/p&gt;

&lt;p&gt;Basically, we’re making things more complicated than they need to be.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Good Practice: Using Derived State
&lt;/h2&gt;

&lt;p&gt;Now, let's apply a bit of React wisdom and fix the above code using &lt;strong&gt;derived state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This time, instead of keeping two separate state variables (&lt;code&gt;products&lt;/code&gt; and &lt;code&gt;filteredProducts&lt;/code&gt;), we'll derive the filtered products directly from the &lt;code&gt;products&lt;/code&gt; array based on the &lt;code&gt;searchQuery&lt;/code&gt;. This way, we avoid redundancy and keep our state clean.&lt;/p&gt;

&lt;p&gt;Here’s the improved version:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&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;ProductList&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;searchQuery&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearchQuery&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="dl"&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;products&lt;/span&gt; &lt;span class="o"&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;id&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Laptop&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Smartphone&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Headphones&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;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Smartwatch&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="c1"&gt;// Derived state: filter products based on the search query&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filteredProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&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;handleSearch&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setSearchQuery&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="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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&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;searchQuery&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;handleSearch&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Search for a product"&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&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;filteredProducts&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;product&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="nt"&gt;li&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;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&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;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;li&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;ul&lt;/span&gt;&lt;span class="p"&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;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ProductList&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;What’s the improvement?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;No extra state for filtered products:&lt;/strong&gt; We no longer store a separate list of &lt;code&gt;filteredProducts&lt;/code&gt;. Instead, we directly compute the filtered list from the &lt;code&gt;products&lt;/code&gt; array and &lt;code&gt;searchQuery&lt;/code&gt; on every render.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cleaner code:&lt;/strong&gt; The component is simpler, with fewer states to manage. This makes it easier to read and maintain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance boost (sort of):&lt;/strong&gt; React doesn’t need to track extra state variables. It just derives the filtered list directly from the existing data, making the code leaner and faster (even though React optimizes updates, fewer state changes are always better).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach takes advantage of &lt;strong&gt;derived state&lt;/strong&gt;, where the filtered products are computed from existing data, instead of storing an additional copy of the filtered data.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Not to Use Derived State
&lt;/h2&gt;

&lt;p&gt;While derived state is often the best choice, it’s not the silver bullet for everything. If your component is dealing with complex calculations or state that’s needed across multiple parts of the application, it might be better to use &lt;strong&gt;memoization&lt;/strong&gt; or store that derived state in a higher-level component or context. &lt;/p&gt;

&lt;p&gt;But for simple filtering, sorting, or other lightweight derived values, the example above should be your go-to approach.&lt;/p&gt;




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

&lt;p&gt;To sum up, derived state in React is all about &lt;strong&gt;keeping things DRY&lt;/strong&gt;—don’t repeat yourself. Instead of keeping unnecessary copies of the same data in state, calculate values on the fly based on existing state and props. This leads to cleaner, more efficient code that’s easier to maintain. So, the next time you’re tempted to duplicate data in React, think about whether you can calculate it directly from other sources. Your future self will thank you!&lt;/p&gt;




</description>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Why You Should Learn Entity Relationship Diagrams Before ORMs and Specific Databases</title>
      <dc:creator>Bruno Henrique</dc:creator>
      <pubDate>Tue, 10 Dec 2024 00:32:25 +0000</pubDate>
      <link>https://dev.to/brunohenrique00/why-you-should-learn-entity-relationship-diagrams-before-orms-and-specific-databases-3l7f</link>
      <guid>https://dev.to/brunohenrique00/why-you-should-learn-entity-relationship-diagrams-before-orms-and-specific-databases-3l7f</guid>
      <description>&lt;p&gt;In the fast-paced world of software development, data modeling often takes a backseat to implementation. &lt;strong&gt;&lt;em&gt;Many developers&lt;/em&gt;&lt;/strong&gt; dive straight into tools like Object-Relational Mappers (ORMs) or specific databases, such as PostgreSQL or even MongoDB, aiming to build functional systems quickly.&lt;/p&gt;

&lt;p&gt;However, skipping the foundational knowledge of Entity Relationship Diagrams (ERDs) can lead to flawed data models, which can propagate bugs and security vulnerabilities throughout an application. &lt;/p&gt;

&lt;p&gt;Here’s why mastering ERDs should be a priority for developers before working with ORMs and databases.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Are Entity Relationship Diagrams (ERDs)?
&lt;/h2&gt;

&lt;p&gt;Well they look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ftn24rpchxe3de49fb9ar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftn24rpchxe3de49fb9ar.png" alt="ERD Diagram" width="800" height="832"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Entity Relationship Diagrams (ERDs) visually represent the structure of a database, mapping entities, attributes, and the relationships between them. They function as blueprints for the logical organization of data and provide a clear representation of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entities&lt;/strong&gt;: Core objects or concepts (e.g., users, products, orders).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attributes&lt;/strong&gt;: Details describing each entity (e.g., name, price, status).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Relationships&lt;/strong&gt;: Connections between entities (e.g., a user places an order).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By understanding ERDs, developers gain insight into how data is interconnected and how it should flow within an application.&lt;/p&gt;

&lt;p&gt;Experienced developers can even understand the business logic when they see an ERD diagram in less time than others.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why ERDs Reduce Bugs in Business Logic
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Relationships Reflect Business Rules&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Every application has business logic that governs how data interacts. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user must have a valid subscription to access premium content.&lt;/li&gt;
&lt;li&gt;An order must belong to a valid user and contain at least one product.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When developers map these relationships in an ERD, they make the business rules explicit. This clarity ensures that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constraints&lt;/strong&gt; (like foreign keys and cardinality rules) are correctly implemented in the database.&lt;/li&gt;
&lt;li&gt;Edge cases are considered during the design phase, reducing the likelihood of unexpected bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Skipping ERDs often results in vague or incorrect mappings, where relationships don’t align with the business logic. This leads to runtime bugs, such as orphaned records or inconsistent data.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Improved Data Integrity&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;ERDs enforce relationships between entities, ensuring data consistency. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A many-to-many relationship between users and roles prevents duplication or missing assignments.&lt;/li&gt;
&lt;li&gt;A one-to-many relationship between customers and orders ensures every order is tied to an existing customer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When these rules are defined and implemented correctly, they eliminate classes of bugs caused by inconsistent or invalid data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why ERDs Help Secure Client Data
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Ensures Proper Data Segmentation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;By mapping out entities and relationships, ERDs make it easier to enforce data segmentation and access controls. For instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sensitive data, such as customer payment details, can be isolated into separate entities.&lt;/li&gt;
&lt;li&gt;Relationships can enforce limited exposure, such as ensuring that an admin can only view or modify specific records.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This systematic organization reduces the risk of data breaches caused by poorly designed schemas where unrelated data is inadvertently exposed.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Supports Auditability&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;ERDs inherently promote a structured approach to data design. By clearly defining relationships, developers can ensure that every action is traceable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Foreign keys and constraints prevent unauthorized or accidental deletions.&lt;/li&gt;
&lt;li&gt;Audit trails can be tied to entity relationships, ensuring every piece of data has an accountable origin and purpose.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This structure is crucial for securing client data, especially in compliance-heavy industries like finance and healthcare.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Ripple Effects of Mastering ERDs
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Fewer Runtime Bugs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Applications often fail at the intersection of data relationships. Bugs like null pointer exceptions, inconsistent query results, and cascading delete failures often arise from poorly defined relationships. ERDs address these issues by defining and enforcing how entities interact before a single line of code is written.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Streamlined Business Logic&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;With ERDs as a reference, developers can implement business rules more effectively. They can identify where constraints should be applied (e.g., maximum number of items in a cart, unique email addresses for users) and avoid redundant or conflicting logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Secure and Scalable Systems&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Security: By clearly defining relationships and data boundaries, ERDs help developers design systems with minimal attack surfaces.&lt;/li&gt;
&lt;li&gt;Scalability: As applications grow, well-structured ERDs simplify the process of adding new features without disrupting existing functionality.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Real-World Example Bug
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Context
&lt;/h4&gt;

&lt;p&gt;Imagine an e-commerce platform where users can place orders. The relationships might be described as follows:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;User&lt;/strong&gt; can place &lt;strong&gt;many Orders&lt;/strong&gt; (one-to-many relationship).&lt;/li&gt;
&lt;li&gt;Each &lt;strong&gt;Order&lt;/strong&gt; must be associated with exactly one &lt;strong&gt;User&lt;/strong&gt; (foreign key in the &lt;code&gt;Orders&lt;/code&gt; table pointing to the &lt;code&gt;Users&lt;/code&gt; table).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Scenario: Order Deletion Cascade
&lt;/h4&gt;

&lt;p&gt;If the relationship between users and orders is incorrectly configured (e.g., no &lt;strong&gt;ON DELETE CASCADE&lt;/strong&gt; or improper handling of dependencies), deleting a user could leave their orders in the database. This leads to:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Inconsistencies&lt;/strong&gt;: Orphaned orders exist, which no longer belong to any user.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Business Logic Errors&lt;/strong&gt;: The system might continue to show these orders in sales reports or dashboards, but they cannot be attributed to any user.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Fix with Proper ERD Design
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Define the relationship:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;User (UserID)&lt;/code&gt; → Primary Key&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Order (OrderID, UserID)&lt;/code&gt; → Foreign Key with &lt;code&gt;NOT NULL&lt;/code&gt; constraint.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;ON DELETE CASCADE&lt;/strong&gt; to ensure that if a user is deleted, their orders are also removed.
&lt;/li&gt;
&lt;li&gt;Verify constraints in the ERD to match real-world business rules:

&lt;ul&gt;
&lt;li&gt;Each order must have exactly one user.&lt;/li&gt;
&lt;li&gt;A user can place multiple orders.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  How to Start Leveraging ERDs
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Learn Core Concepts&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Key attributes (primary and foreign keys)&lt;/li&gt;
&lt;li&gt;Types of relationships (one-to-one, one-to-many, many-to-many)&lt;/li&gt;
&lt;li&gt;Normalization to avoid redundancy&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Practice with Tools&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Start with tools like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lucidchart&lt;/strong&gt; or &lt;strong&gt;Draw.io&lt;/strong&gt; for general diagramming.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;dbdiagram.io&lt;/strong&gt; or &lt;strong&gt;MySQL Workbench&lt;/strong&gt; for database-specific modeling.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Apply ERDs to Projects&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before coding, create an ERD for every new project. Revisit and refine your diagrams as business requirements evolve.&lt;/p&gt;




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

&lt;p&gt;Mastering Entity Relationship Diagrams is more than just a technical skill—it’s a mindset shift toward structured and intentional system design if &lt;em&gt;&lt;strong&gt;you want to be a real Senior Software Engineer&lt;/strong&gt;&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;By understanding how relationships dictate business logic, developers can avoid common pitfalls like inefficient queries, data corruption, and security vulnerabilities. ERDs lay the groundwork for scalable, maintainable, and secure systems, giving developers the tools to design systems that meet both technical and business needs.&lt;/p&gt;

&lt;p&gt;Invest in learning ERDs first, and you’ll not only write better code but also deliver systems that are reliable, secure, and future-proof.&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>software</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
