<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[React and Next js Blogs]]></title><description><![CDATA[React and Next js Blogs]]></description><link>https://react-nextjs.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/695291ab5b12442dcb8f69d8/c152f706-41e2-43a4-90c5-bb73876a5ed2.png</url><title>React and Next js Blogs</title><link>https://react-nextjs.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Mon, 02 Nov 2026 18:55:52 GMT</lastBuildDate><atom:link href="https://react-nextjs.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[React Fundamentals]]></title><description><![CDATA[Components, JSX, State, and Re-rendering]]></description><link>https://react-nextjs.hashnode.dev/react-fundamentals</link><guid isPermaLink="true">https://react-nextjs.hashnode.dev/react-fundamentals</guid><category><![CDATA[React]]></category><dc:creator><![CDATA[Siddhartha Mohapatra]]></dc:creator><pubDate>Sun, 01 Nov 2026 17:58:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/695291ab5b12442dcb8f69d8/c52af268-e63a-46bc-9c7d-b8ee49b3a080.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3><em>"Why did developers create React when JavaScript already existed?"</em></h3>
<p>Before React, JavaScript was used for fetching some data, finding some element using <code>document.getElementById()</code> adding or removing CSS classes, updating inner text, and attaching event listeners.</p>
<p>This works well with small applications, but when it comes to large-scale applications that handle user profiles, live feeds, notifications, and forms, it faces problems syncing the UI, as a single state change might require updating five different places in the DOM. If we miss one, the UI gets out of sync.</p>
<p>To fix this issue, Facebook created React in the year of 2013 to solve this UI complexity Instead of manually reaching into the browser's DOM to manipulate elements step-by-step, React allows developers to describe <strong>what</strong> the UI should look like for any given state, and automatically handles all the updates under the hood.</p>
<h2>1. Why React Exists</h2>
<h3>Problems with Traditional DOM Manipulation</h3>
<p>In traditional web development, you use <strong>imperative programming</strong>. You give the browser step-by-step instructions on how to change the DOM:</p>
<pre><code class="language-javascript">// Imperative vanilla JS
const button = document.getElementById('like-btn');
button.addEventListener('click', () =&gt; {
  count++;
  const countSpan = document.getElementById('like-count');
  countSpan.innerText = count;
  button.classList.add('liked');
});
</code></pre>
<p>As user interfaces grow, managing dependencies between raw DOM elements leads to "spaghetti code." It becomes extremely hard to track which event handler modified which element.</p>
<h3>The Component-Driven Approach</h3>
<p>React introduced <strong>Component-Driven Development</strong>. In this Instead of viewing a web page as a single massive document, React treats UI as a collection of independent, reusable pieces called <strong>components</strong>.</p>
<p>Whether it is a <strong>User Profile</strong>, a <strong>Product Card</strong>, a <strong>Social Media Post</strong>, or a <strong>Dashboard Widget</strong>, complex interfaces are broken down into smaller, self-contained building blocks.</p>
<p>Components are portable and shareable.</p>
<p>Syntax-</p>
<pre><code class="language-javascript">function App (){
//Todo
  return(
    //JSX - TSX
 )
}
</code></pre>
<h3>What is JSX?</h3>
<p>JSX stands for <strong>JavaScript XML (JS + HTML)</strong>. It is a syntax extension for JavaScript that allows you to write HTML-like markup directly inside JavaScript files.</p>
<pre><code class="language-javascript">const userCard = (
  &lt;div className="user-card"&gt;
    &lt;h2&gt;Siddhartha Mohapatra&lt;/h2&gt;
    &lt;p&gt;Software Engineer&lt;/p&gt;
  &lt;/div&gt;
);
</code></pre>
<h3>Why JSX Was Introduced</h3>
<p>Before React, HTML for structure, CSS for styles, and JS for logic. However, modern interactive UIs tightly couple rendering logic with user interaction logic. JSX allows you to keep component UI and logic together in one place.</p>
<h3>JSX vs HTML</h3>
<p>While JSX looks like HTML, it is stricter and has a few key differences:</p>
<ul>
<li><code>className</code> <strong>instead of</strong> <code>class</code>: Since <code>class</code> is a reserved keyword in JavaScript, React uses <code>className</code>.</li>
<li><strong>Self-closing tags</strong>: All tags must be explicitly closed (e.g., <code>&lt;img /&gt;</code> or <code>&lt;input /&gt;</code>).</li>
<li><strong>CamelCase attributes</strong>: Attributes like <code>onclick</code> become <code>onClick</code>, and <code>tabindex</code> becomes <code>tabIndex</code>.</li>
</ul>
<h3>Embedding JavaScript inside JSX</h3>
<p>You can embed any valid JavaScript expression inside JSX by wrapping it in curly braces <code>{}</code>:</p>
<pre><code class="language-javascript">const username = "Siddhartha";
const unreadMessages = 5;

return (
  &lt;div&gt;
    &lt;h1&gt;Welcome back, {username}!&lt;/h1&gt;
    &lt;p&gt;You have {unreadMessages * 2} pending notifications.&lt;/p&gt;
  &lt;/div&gt;
);
</code></pre>
<h3>High-Level Compilation Process</h3>
<p>Browsers do not understand JSX directly. A compiler like <strong>Babel</strong> transpiles JSX into standard JavaScript function calls <code>React.createElement</code>):</p>
<pre><code class="language-javascript">JSX Input:
&lt;h1 className="title"&gt;Hello World&lt;/h1&gt;

Compiled Output:
React.createElement('h1', { className: 'title' }, 'Hello World');
</code></pre>
<h2>3. Components in React</h2>
<h3>What is a Component?</h3>
<p>A React component is a JavaScript function that returns JSX. Components act as custom HTML elements with their own visual structure and logic.</p>
<pre><code class="language-javascript">// A simple function component
function ProductCard() {
  return (
    &lt;div className="product-card"&gt;
      &lt;img src="laptop.jpg" alt="Laptop" /&gt;
      &lt;h3&gt;Wireless Headphones&lt;/h3&gt;
      &lt;p&gt;$99.99&lt;/p&gt;
      &lt;button&gt;Add to Cart&lt;/button&gt;
    &lt;/div&gt;
  );
}
export default ProductCard
</code></pre>
<h3>Component Composition</h3>
<p>You can combine smaller components to construct larger UIs. For example, a <strong>Social Media Feed</strong> can be composed of multiple <strong>Post</strong> components, which in turn contain <strong>Avatar</strong>, <strong>LikeButton</strong>, and <strong>Comment</strong> components.</p>
<pre><code class="language-javascript">function SocialFeed() {
  return (
    &lt;div className="feed"&gt;
      &lt;Post /&gt;
      &lt;Post /&gt;
      &lt;Post /&gt;
    &lt;/div&gt;
  );
}
export default SocialFeed
</code></pre>
<h2>4. Props in React</h2>
<h3>What are Props?</h3>
<p><code>Props</code> (short for properties) are inputs passed into React components. They make components dynamic and reusable rather than hardcoded.</p>
<h3>Passing and Using Props</h3>
<p>Data flows sequentially from <strong>Parent to Child</strong> components via props:</p>
<pre><code class="language-javascript">// Child Component with destructured props
function ProfileCard({ name, age, isAdmin }) {
  return (
    &lt;div className="card"&gt;
      &lt;h2&gt;Name: {name}&lt;/h2&gt;
      &lt;p&gt;Age: {age}&lt;/p&gt;
      {isAdmin &amp;&amp; &lt;span&gt;Admin User&lt;/span&gt;}
    &lt;/div&gt;
  );
}

export default ProfileCard
</code></pre>
<pre><code class="language-javascript">// Using ProfileCard component in APP.Jsx / Tsx
import ProfileCard from './ProfileCard.Jsx';

function App() {
  return (
    &lt;div&gt;
      {/* Passing different values to the same component */}
      &lt;ProfileCard name="Alice" age={28} isAdmin={true} /&gt;
      &lt;ProfileCard name="Bob" age={34} isAdmin={false} /&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>Props are <strong>read-only</strong> (immutable). A child component must never modify the props it receives. If the data needs to change over time in response to user actions, put it in <strong>State</strong>.</p>
<p><strong>Default Values:</strong> You can assign default parameter values right inside your destructured function arguments to handle missing values gracefully.</p>
<pre><code class="language-javascript">function Button({ text = "Click Me", color = "blue" }) {
  return &lt;button style={{ backgroundColor: color }}&gt;{text}&lt;/button&gt;;
}
</code></pre>
<h2>5. State in React</h2>
<h3>What is State?</h3>
<p>While <code>props</code> allow a parent to pass data down to a child, <code>state</code> is a component's personal memory. State holds data that changes over time based on user interaction, network responses, or timers.</p>
<h3>Local Component State (<code>useState</code>)</h3>
<p>React provides a hook called <code>useState</code> to attach local state to a function component:</p>
<pre><code class="language-javascript">import { useState } from 'react';

function LikeButton() {
  // Declare a state variable named 'likes' initialized to 0
  const [likes, setLikes] = useState(0);

  function handleClick() {
    setLikes(likes + 1); // Updating state
  }

  return (
    &lt;button onClick={handleClick}&gt;
      ❤️ Likes: {likes}
    &lt;/button&gt;
  );
}
</code></pre>
<h3>State-Driven UI</h3>
<p>In React, you don't update DOM elements manually when data changes. You simply update the state, and React automatically updates the UI to reflect the new state.</p>
<h2>6. Understanding Re-rendering</h2>
<h3>What Causes a Re-render?</h3>
<p>A re-render occurs when React calls a component function again to determine what changes need to be made on screen. The primary triggers for a re-render are:</p>
<ol>
<li><strong>State updates</strong>: Calling a state updater function (e.g., <code>setLikes</code>).</li>
<li><strong>Prop changes</strong>: When a parent component passes new prop values down to a child.</li>
<li><strong>Parent re-rendering</strong>: When a parent component re-renders, all its child components re-render by default.</li>
</ol>
<h3>The Mental Model for Re-rendering</h3>
<ol>
<li><strong>Trigger</strong>: User clicks a button, triggering a state update.</li>
<li><strong>Render</strong>: React runs the component function to get the updated JSX structure.</li>
<li><strong>Commit</strong>: React compares the new JSX with the previous render (diffing) and updates <em>only</em> the modified parts of the actual browser DOM.</li>
</ol>
<h2>7. React's Declarative Nature</h2>
<h3>Imperative vs Declarative</h3>
<ul>
<li><p><strong>Imperative (Vanilla JS)</strong>: Focuses on <em>how</em> to achieve a result, step by step.</p>
<blockquote>
<p>"Find button #1, listen for click, get text box #2, read text, create new <code>&lt;li&gt;</code>, append <code>&lt;li&gt;</code> to list <code>#3</code>."</p>
</blockquote>
</li>
<li><p><strong>Declarative (React)</strong>: Focuses on <em>what</em> the final UI should look like based on the current state.</p>
<blockquote>
<p>"Here is an array of items in state. Render an <code>&lt;li&gt;</code> for every item in this array."</p>
</blockquote>
</li>
</ul>
<h3>Predictable UI Updates</h3>
<p>By declaring what the UI should look like for state \(S_1\) versus state \(S_2\), React eliminates bugs caused by forgotten DOM updates or inconsistent UI states.</p>
<h2>8. Component Tree Architecture</h2>
<p>React applications are structured as a hierarchy of components:</p>
<pre><code class="language-plaintext">App
 ├── Header
 │    └── UserProfile
 └── Dashboard
      ├── Widget (Users)
      ├── Widget (Revenue)
      └── ProductCardList
           ├── ProductCard
           └── ProductCard
</code></pre>
<ul>
<li><strong>Data Flow</strong>: Data flows unidirectionally <strong>downwards</strong> from parent components to child components via props.</li>
<li><strong>Component Hierarchy</strong>: Keeps application structure modular, organized, and scalable.</li>
</ul>
<h2>9. Common Beginner Mistakes</h2>
<ol>
<li><strong>Mutating State Directly</strong>:<ul>
<li>❌ <code>state.count = 5;</code> (React will not detect this change or trigger a re-render).</li>
<li>✅ <code>setCount(5);</code></li>
</ul>
</li>
<li><strong>Confusing Props and State</strong>:<ul>
<li><strong>Props</strong>: Passed into a component from above (read-only).</li>
<li><strong>State</strong>: Managed inside the component (mutable via state setter).</li>
</ul>
</li>
<li><strong>Overusing State</strong>:<ul>
<li>Do not put values into state if they can be calculated on the fly during rendering (e.g., calculating <code>fullName</code> from <code>firstName</code> and <code>lastName</code>).</li>
</ul>
</li>
<li><strong>Creating Monolithic Components</strong>:<ul>
<li>Keeping hundreds of lines of UI and logic in a single file instead of breaking them into smaller sub-components.</li>
</ul>
</li>
</ol>
<h2>10. Building Applications with Components</h2>
<p>When building an application in React, use the <strong>Thinking in React</strong> methodology:</p>
<ol>
<li><strong>Break the UI into a Component Hierarchy</strong>: Draw boxes around every component and subcomponent in your mockups.</li>
<li><strong>Build a Static Version in React</strong>: Create components that take props and render UI without adding state yet.</li>
<li><strong>Identify the Minimal Representation of UI State</strong>: Figure out the absolute minimum state your application needs.</li>
<li><strong>Determine Where Your State Should Live</strong>: Identify which component owns or holds the state.</li>
<li><strong>Add Inverse Data Flow</strong>: Pass down callback functions through props so child components can request state changes from parents.</li>
</ol>
<hr />
<p><em>If you want to practice or prepare for a React interview, or are just learning, I highly recommend you check out React Labs by</em> <a href="https://react.chaicode.com/">Chaicode.com</a></p>
]]></content:encoded></item></channel></rss>