React Components Architecture
A masterclass tutorial on What is a Component, Functional Components, PascalCase Naming Conventions, Composition, Reusability, Parent-Child Hierarchy, Default vs Named Exports, Common Mistakes, Interview Questions, and Building a Full Multi-Component Layout Architecture.
What is a React Component?
A Component is the fundamental building block of a React user interface.
In traditional web development, a webpage is built as one monolithic HTML file. In React, a webpage is broken down into small, isolated, reusable pieces of UI called Components.
Analogy:
Think of a React component like a LEGO brick. You can create individual bricks (buttons, headers, cards) and assemble them together to build complex structures (dashboards, e-commerce stores, social media feeds).
Technically, a React Component is a JavaScript function that accepts inputs (called Props) and returns a React element tree (written in JSX).
Functional Components vs. Class Components
React components can be written in two ways:
- 1. Functional Components (Modern Standard):
Simple JavaScript functions that return JSX. Since React 16.8, functional components can manage state and side effects using React Hooks.
function Welcome() {
return <h1>Welcome to React!</h1>;
}- 2. Class Components (Legacy / Historical):
ES6 JavaScript classes extending React.Component requiring a render() method.
class Welcome extends React.Component {
render() {
return <h1>Welcome to React!</h1>;
}
}Industry Standard: Modern React codebases exclusively use Functional Components with Hooks. Class components are considered legacy.
Component Naming Conventions (PascalCase Rule)
React Component names MUST start with a Capital Letter (PascalCase).
- Correct:
<Navbar />,<ProductCard />,<UserProfile /> - Incorrect:
<navbar />,<productCard />,<user_profile />
Why PascalCase is Mandatory:
React uses the first letter of a JSX tag to determine whether it is dealing with a built-in HTML tag or a custom React component:
- Lowercase tags (
<div />,<button />,<p />) are compiled to standard HTML strings:React.createElement('div'). - Capitalized tags (
<Navbar />) are compiled as JavaScript component functions:React.createElement(Navbar).
Component Composition & Reusability
Component Composition is the practice of combining small components to form larger components.
Why Reusability Matters:
Imagine you need to display 100 product items on an e-commerce website. Without components, you would duplicate 100 blocks of HTML code. With a reusable <ProductCard /> component, you write the layout once and reuse it 100 times with different data.
Parent and Child Components & Component Hierarchy
React components organize into a Parent-Child Tree Hierarchy:
- Parent Component: A component that renders another component inside its return statement.
- Child Component: A component nested inside a parent component.
[ App (Parent Root) ]
/ | \
[ Navbar ] [ Hero ] [ Footer ]
|
[ ProductList ]
|
[ ProductCard ] (Child)Importing and Exporting Components (Modules)
React applications use ES6 JavaScript modules (import and export) to split code across separate files.
Default Exports (export default)
A file can have only ONE default export.
Export Syntax:
// Navbar.jsx
export default function Navbar() {
return <nav>My Navbar</nav>;
}Import Syntax:
When importing a default export, you do not use curly braces. You can even rename the imported component if desired:
// App.jsx
import Navbar from './Navbar';
// OR import MyCustomNavbar from './Navbar';Named Exports (export const)
A file can have MULTIPLE named exports.
Export Syntax:
// Buttons.jsx
export function PrimaryButton() {
return <button className="btn-primary">Click Me</button>;
}
export function SecondaryButton() {
return <button className="btn-secondary">Cancel</button>;
}Import Syntax:
Named exports MUST be imported inside curly braces {} matching the exact exported name:
// App.jsx
import { PrimaryButton, SecondaryButton } from './Buttons';Default vs. Named Exports Comparison
| Feature | Default Export | Named Export |
|---|---|---|
| Exports per file | Exactly One (1) | Unlimited (N) |
| Export Syntax | export default ComponentName | export const ComponentName |
| Import Syntax | import ComponentName from './file' | import { ComponentName } from './file' |
| Rename on Import? | Yes (import Custom from './file') | No (Requires as syntax: import { A as B }) |
Common Mistakes with Components
- Defining a Component inside another Component: Never nest component function definitions inside another component function; it causes full component re-instantiation on every render.
- Forgetting to return JSX: A component must return valid JSX or
null. - Using Lowercase Names: Writing
<buttonCard />instead of<ButtonCard />.
Top React Interview Questions on Components
Q1: What is a Component in React?
*Answer*: A component is a self-contained, reusable block of UI logic and template written in JavaScript (JSX) that returns React elements.
Q2: What is the difference between Default Export and Named Export?
*Answer*: A file can have only one default export (imported without {}), whereas a file can have multiple named exports (imported with {} using exact names).
Practical Hands-On Exercise โ Building App Component Architecture Tree
Let's build a complete multi-component application matching this exact architecture hierarchy:
๐ src/
โโโ ๐ App.jsx (Root Parent) โโโ ๐ components/ โ โโโ ๐ Navbar.jsx โ โโโ ๐ Hero.jsx โ โโโ ๐ ProductList.jsx โ โโโ ๐ ProductCard.jsx โ โโโ ๐ Footer.jsx
// 1. src/components/Navbar.jsx
import React from 'react';
export default function Navbar() {
return (
<nav className="navbar" style={{ padding: "15px", background: "#002057", color: "#fff" }}>
<h2>E-Commerce Store</h2>
</nav>
);
}
// 2. src/components/Hero.jsx
import React from 'react';
export default function Hero() {
return (
<div className="hero" style={{ padding: "30px", background: "#f1f5f9", textAlign: "center" }}>
<h1>Featured Deals ๐๏ธ</h1>
<p>Explore high quality tech gadgets built with React Component Architecture!</p>
</div>
);
}
// 3. src/components/ProductCard.jsx
import React from 'react';
export function ProductCard({ name, price }) {
return (
<div style={{ border: "1px solid #cbd5e1", borderRadius: "10px", padding: "15px", margin: "10px 0" }}>
<h4>{name}</h4>
<p>Price: โน{price}</p>
</div>
);
}
// 4. src/components/ProductList.jsx
import React from 'react';
import { ProductCard } from './ProductCard';
export default function ProductList() {
return (
<div className="product-list" style={{ padding: "20px" }}>
<h3>Product Catalog</h3>
<ProductCard name="iPhone 15 Pro" price={129999} />
<ProductCard name="MacBook Air M3" price={114900} />
</div>
);
}
// 5. src/components/Footer.jsx
import React from 'react';
export default function Footer() {
return (
<footer style={{ padding: "15px", background: "#0f172a", color: "#fff", textAlign: "center" }}>
<p>Copyright ยฉ 2026 Ravindra Portfolio Store</p>
</footer>
);
}
// 6. src/App.jsx (Root Composition)
import React from 'react';
import Navbar from './components/Navbar';
import Hero from './components/Hero';
import ProductList from './components/ProductList';
import Footer from './components/Footer';
export default function App() {
return (
<div>
<Navbar />
<Hero />
<ProductList />
<Footer />
</div>
);
}