<?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: Lucas Nahas</title>
    <description>The latest articles on DEV Community by Lucas Nahas (@lucas_cn).</description>
    <link>https://dev.to/lucas_cn</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%2F2895791%2Fc8ddbe3e-5222-4353-a66d-645e4b5f944b.png</url>
      <title>DEV Community: Lucas Nahas</title>
      <link>https://dev.to/lucas_cn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lucas_cn"/>
    <language>en</language>
    <item>
      <title>Local vs. Shared Components – Striking the Right Balance</title>
      <dc:creator>Lucas Nahas</dc:creator>
      <pubDate>Wed, 26 Feb 2025 22:05:58 +0000</pubDate>
      <link>https://dev.to/lucas_cn/local-vs-shared-components-striking-the-right-balance-3g61</link>
      <guid>https://dev.to/lucas_cn/local-vs-shared-components-striking-the-right-balance-3g61</guid>
      <description>&lt;p&gt;Have you ever spent ages looking for the TV remote, only to find it on top of the fridge or buried under a pile of laundry—nowhere near the actual TV? You search every logical place, but it’s just not where it should be.  &lt;/p&gt;

&lt;p&gt;Working with poorly structured code feels the same. You waste time searching instead of actually getting things done.  &lt;/p&gt;

&lt;p&gt;A well-organized codebase isn’t just about keeping things tidy—it’s about making sure everything has its place, is easy to find, and serves its purpose efficiently. Nowhere is this more important than in how you structure your components.  &lt;/p&gt;

&lt;p&gt;One of the biggest decisions in organizing a React codebase is knowing when a component should remain &lt;strong&gt;local&lt;/strong&gt; and when it should be made &lt;strong&gt;shared&lt;/strong&gt;. Get this wrong, and you either end up with cluttered, redundant code or an overly abstracted mess that’s difficult to work with.  &lt;/p&gt;

&lt;p&gt;In this post, we’ll explore the difference between these two types of components, common pitfalls, and how to find the right balance.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Local vs. Shared Components - What are they
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;local component&lt;/strong&gt; is a subcomponent used only within a specific parent component or feature. &lt;/p&gt;

&lt;p&gt;It is tightly coupled to that component and isn’t meant to be shared elsewhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   # Local Structure --&amp;gt; Hierarchy

   components/  
   │── ImageGrid/  
   │   ├── ImageGrid.tsx # Main Component 
   │   ├── Card.tsx      # Local Component  
   │   ├── Button.tsx    # Local Component  
   │  
   │── Form/  
   │   ├── Form.tsx    # Main Component 
   │   ├── Button.tsx  # Local Component  
   │  
   │── OtherComponent.tsx  

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;strong&gt;shared component&lt;/strong&gt;, on the other hand, is designed to be used across multiple parent components.&lt;/p&gt;

&lt;p&gt;It serves a more generalized purpose and isn’t tied to a single component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   # Shared Structure --&amp;gt; No Hierarchy

   components/  
   │── ImageGrid.tsx  
   │── Card.tsx  
   │── Button.tsx  
   │── Form.tsx  
   │── OtherComponent.tsx  

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Let's Exemplify
&lt;/h2&gt;

&lt;p&gt;Imagine you’re working on a project that includes an image grid and a form. &lt;/p&gt;

&lt;p&gt;The image grid displays a collection of images, with each item represented as a card component, which itself contains a button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   ImageGrid
      ↳ Card
         ↳ Button
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Meanwhile, the form contains fields and a button component that is identical to the one used in the image grid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Form
      ↳ Button
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two different approaches could be used when structuring your components in a project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You could use a &lt;strong&gt;local&lt;/strong&gt; approach, where every component would live 
inside its respective folder. The ImageGrid folder contains the ImageGrid, Card, and Button components, while the Form folder contains the Form and Button components. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You could use a &lt;strong&gt;shared component&lt;/strong&gt; approach, where all components are placed at the root level. This eliminates duplication, since the Button component is now shared. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both methods seem reasonable. But as the project grows, problems start to appear. Either strategy introduces issues, as we are about to see.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem With a Flat Structure (Shared Approach)
&lt;/h2&gt;

&lt;p&gt;Placing all components in a single folder may seem simple and straighforward. However, as your codebase grows, it quickly becomes unmanageable.&lt;/p&gt;

&lt;p&gt;Subcomponents get mixed with larger parent components. There’s no clear hierarchy, making it hard to tell which components belong to which features.&lt;/p&gt;

&lt;p&gt;Searching for specific components becomes frustrating and time-consuming.&lt;br&gt;
This is like throwing all your tools into a single box without any organization.&lt;/p&gt;

&lt;p&gt;Sure, everything is in one place—but finding the exact tool you need becomes a headache.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem With an Overly Strict Hierarchy
&lt;/h2&gt;

&lt;p&gt;On the other hand, taking an extremely rigid, local approach also introduces issues.&lt;/p&gt;

&lt;p&gt;You carefully place each component inside its respective folder, &lt;br&gt;
ensuring modularity. But what happens when two components need the same button?&lt;/p&gt;

&lt;p&gt;Instead of reusing a single component, two identical are created. Now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code is duplicated unnecessarily.&lt;/li&gt;
&lt;li&gt;Updating the button’s design requires changing multiple instances.&lt;/li&gt;
&lt;li&gt;Searching for the "main" button becomes difficult in a large codebase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At first, this structure feels intuitive, but as the project scales, it leads to inconsistencies and wasted effort—the very thing a strict structure is meant to prevent.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Solve this Issue
&lt;/h2&gt;

&lt;p&gt;Start with local components. Keep them scoped to the feature they belong to.  &lt;/p&gt;

&lt;p&gt;If you find yourself needing the same component elsewhere, promote it to a shared component only when necessary.  &lt;/p&gt;

&lt;p&gt;This approach:&lt;br&gt;&lt;br&gt;
✔ Keeps your codebase clean and structured.&lt;br&gt;
✔ Avoids premature abstraction.&lt;br&gt;
✔ Ensures reusability happens naturally as the project evolves.&lt;/p&gt;

&lt;p&gt;Balancing local and shared components is crucial for maintainable React code.&lt;/p&gt;

&lt;p&gt;By structuring your components thoughtfully, you can reduce complexity, &lt;br&gt;
prevent duplication, and make your codebase easier to work with.&lt;/p&gt;

&lt;p&gt;Neither a purely local nor a fully shared approach works perfectly on its own.&lt;/p&gt;

&lt;p&gt;A well-structured codebase strikes a balance—keeping components scoped to &lt;br&gt;
their parent components while promoting reusability only when necessary.&lt;/p&gt;

&lt;p&gt;How do you structure your react projects?&lt;/p&gt;

&lt;p&gt;Let's discuss it in the comments!&lt;/p&gt;

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