<?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: Manisha Mardi</title>
    <description>The latest articles on Forem by Manisha Mardi (@manisha_mardi).</description>
    <link>https://forem.com/manisha_mardi</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%2F1504788%2Fde78f810-712c-43dd-b5c5-8ddc47950d20.png</url>
      <title>Forem: Manisha Mardi</title>
      <link>https://forem.com/manisha_mardi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/manisha_mardi"/>
    <language>en</language>
    <item>
      <title>Thinking in React: A Beginner’s Journey</title>
      <dc:creator>Manisha Mardi</dc:creator>
      <pubDate>Wed, 11 Dec 2024 17:21:03 +0000</pubDate>
      <link>https://forem.com/manisha_mardi/thinking-in-react-a-beginners-journey-1pe4</link>
      <guid>https://forem.com/manisha_mardi/thinking-in-react-a-beginners-journey-1pe4</guid>
      <description>&lt;p&gt;Building user interfaces with React involves a structured approach that enhances both development efficiency and code maintainability. This guide delves into essential concepts and methodologies to help beginners navigate React development effectively. Here, I’ll share my key learnings, sprinkled with simple examples to help you get started.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. Breaking the UI into a Component Hierarchy
&lt;/h3&gt;

&lt;p&gt;React applications are like puzzles: you need to identify the pieces before assembling them. Breaking down your UI into components helps you modularize the design and focus on reusability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Imagine building a shopping cart interface. Here’s how I approached it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Parent Component:&lt;/strong&gt; &lt;code&gt;ShoppingCart&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Child Components:&lt;/strong&gt; &lt;code&gt;CartItem&lt;/code&gt;, &lt;code&gt;CartSummary&lt;/code&gt;, &lt;code&gt;PromoCodeForm&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each component has a specific responsibility. For example, &lt;code&gt;CartItem&lt;/code&gt; handles the display of individual items, while &lt;code&gt;CartSummary&lt;/code&gt; calculates the total price. Starting with a sketch or mockup makes this process easier.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Building a Static Version
&lt;/h3&gt;

&lt;p&gt;Before diving into state and interactivity, it’s crucial to create a static version of your application. This step focuses solely on rendering the UI based on fixed data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;If the shopping cart has three items, render them using hardcoded data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function CartItem({ name, price }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;{name}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;${price}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

function ShoppingCart() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;CartItem name="T-shirt" price={20} /&amp;gt;
      &amp;lt;CartItem name="Jeans" price={40} /&amp;gt;
      &amp;lt;CartItem name="Shoes" price={60} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach ensures the UI layout is solid before you integrate logic.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Identifying Minimal State
&lt;/h3&gt;

&lt;p&gt;State is the dynamic part of React. Deciding what should be stateful is essential. The rule of thumb is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If data changes over time, it’s state.&lt;/li&gt;
&lt;li&gt;If data is passed unchanged from parent to child, it’s a prop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;In the shopping cart, the &lt;strong&gt;promo code&lt;/strong&gt; and &lt;strong&gt;quantity&lt;/strong&gt; are stateful because they can change. However, the product names and prices are static data (props).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function PromoCodeForm({ promoCode, setPromoCode }) {
  return (
    &amp;lt;input
      type="text"
      value={promoCode}
      onChange={(e) =&amp;gt; setPromoCode(e.target.value)}
      placeholder="Enter Promo Code"
    /&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;promoCode&lt;/code&gt; state is managed in the parent component and updated via &lt;code&gt;setPromoCode&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Adding Inverse Data Flow
&lt;/h3&gt;

&lt;p&gt;React’s unidirectional data flow ensures clarity, but sometimes child components need to update parent state. This is achieved using callback functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;If the user updates the quantity of a cart item, the parent component’s state should reflect the change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function CartItem({ name, price, quantity, onQuantityChange }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;{name}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;${price}&amp;lt;/p&amp;gt;
      &amp;lt;input
        type="number"
        value={quantity}
        onChange={(e) =&amp;gt; onQuantityChange(e.target.value)}
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

function ShoppingCart() {
  const [cart, setCart] = React.useState([
    { id: 1, name: "T-shirt", price: 20, quantity: 1 },
    { id: 2, name: "Jeans", price: 40, quantity: 1 },
  ]);

  const handleQuantityChange = (id, newQuantity) =&amp;gt; {
    setCart((prevCart) =&amp;gt;
      prevCart.map((item) =&amp;gt;
        item.id === id ? { ...item, quantity: newQuantity } : item
      )
    );
  };

  return (
    &amp;lt;div&amp;gt;
      {cart.map((item) =&amp;gt; (
        &amp;lt;CartItem
          key={item.id}
          name={item.name}
          price={item.price}
          quantity={item.quantity}
          onQuantityChange={(newQuantity) =&amp;gt;
            handleQuantityChange(item.id, newQuantity)
          }
        /&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  5. Adding Interactivity
&lt;/h3&gt;

&lt;p&gt;With state in place, interactivity comes to life. Event handlers like &lt;code&gt;onClick&lt;/code&gt; or &lt;code&gt;onChange&lt;/code&gt; can update the state, making the application dynamic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Adding a "Remove Item" button in the cart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={() =&amp;gt; handleRemoveItem(item.id)}&amp;gt;Remove&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;handleRemoveItem&lt;/code&gt; function filters the item out of the cart array and updates the state.&lt;/p&gt;




&lt;h3&gt;
  
  
  6. Props vs State
&lt;/h3&gt;

&lt;p&gt;This was a game-changer for me. Props are read-only and flow from parent to child, while state is managed within the component. Understanding this distinction helped me debug faster and design better components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick Rule:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;props&lt;/strong&gt; for immutable data.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;state&lt;/strong&gt; for data that needs to change.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. Top-Down vs Bottom-Up Development
&lt;/h3&gt;

&lt;p&gt;Both approaches have their place:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Top-Down:&lt;/strong&gt; Start with the big picture and drill down into smaller components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bottom-Up:&lt;/strong&gt; Build foundational components first and integrate them into the larger structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For simple apps, I prefer top-down. For complex projects, bottom-up ensures I get the basics right.&lt;/p&gt;




&lt;h3&gt;
  
  
  8. Iterative Development
&lt;/h3&gt;

&lt;p&gt;React shines when you build incrementally. Start simple, test often, and refine as you go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First, render a static product list.&lt;/li&gt;
&lt;li&gt;Next, add filtering functionality.&lt;/li&gt;
&lt;li&gt;Finally, enable sorting or search features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each step builds on the last, ensuring a robust application.&lt;/p&gt;




&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;Thinking in React transformed how I approach front-end development. By breaking problems into manageable parts and iterating step-by-step, I’ve been able to build applications that are not only functional but also maintainable.&lt;/p&gt;

&lt;p&gt;If you’re just starting out, embrace this systematic approach. Trust me, it will save you countless headaches and make you a better developer.&lt;/p&gt;

&lt;p&gt;Let me know your thoughts! How do you approach building React applications?&lt;/p&gt;

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