Components Architecture

Overview

The components layer provides reusable UI building blocks organized into shared components (buttons, modals, loaders) and page-specific components. All components follow React best practices with proper TypeScript typing, error boundaries, and accessibility.

Quick Navigation

  • Shared Components - Reusable components (Header, Sidebar, Footer, Buttons, ErrorBoundary, HelpModal, SkeletonLoader)
  • Page Components - Page-level components and route mapping (9 pages)
  • Styling - CSS hierarchy, Tailwind CSS, responsive design patterns
  • Testing - Component testing patterns, accessibility testing, snapshot testing
  • Overview - This file

Component Hierarchy

src/components/
β”œβ”€β”€ Buttons.tsx           # Reusable button components
β”œβ”€β”€ ErrorBoundary.tsx     # Error boundary wrapper
β”œβ”€β”€ Footer.tsx            # Footer component
β”œβ”€β”€ Header.tsx            # Header with navigation
β”œβ”€β”€ HelpModal.tsx         # Help documentation modal
β”œβ”€β”€ Sidebar.tsx           # Navigation sidebar
└── SkeletonLoader.tsx    # Loading placeholder

src/pages/
β”œβ”€β”€ AdminDashboard.tsx    # Admin management interface
β”œβ”€β”€ ChangeProductDetailsPage.tsx
β”œβ”€β”€ DeleteProductPage.tsx
β”œβ”€β”€ HomePage.tsx          # Landing page
β”œβ”€β”€ ListStockPage.tsx     # Product inventory list
β”œβ”€β”€ LoginPage.tsx         # Authentication
β”œβ”€β”€ SearchProductPage.tsx
β”œβ”€β”€ UserDashboard.tsx     # User dashboard
└── AddProductPage.tsx    # Product creation

Data Flow

App.tsx (Root)
β”œβ”€β”€ Router (React Router)
β”‚   β”œβ”€β”€ Layout Components
β”‚   β”‚   β”œβ”€β”€ Header (Navigation, User Menu)
β”‚   β”‚   β”œβ”€β”€ Sidebar (Route Links)
β”‚   β”‚   └── Footer (Static Info)
β”‚   β”‚
β”‚   └── Page Components (Routed)
β”‚       β”œβ”€β”€ LoginPage
β”‚       β”œβ”€β”€ HomePage
β”‚       β”œβ”€β”€ ListStockPage
β”‚       β”œβ”€β”€ AdminDashboard
β”‚       └── ... (other pages)
β”‚
└── Redux Store (Global State)
    β”œβ”€β”€ Products State
    β”œβ”€β”€ User/Auth State
    └── UI State (Modals, Notifications)

Component Types

1. Shared/Layout Components

Used across multiple pages:

Component Purpose Props
Header Top navigation, user menu navigation links
Sidebar Left sidebar navigation routes, collapsed state
Footer Bottom footer static content
Buttons Action buttons variant, onClick, disabled
ErrorBoundary Catch React errors children, fallback
HelpModal Help documentation isOpen, onClose
SkeletonLoader Loading state count, variant

2. Page Components

Route-specific components:

Page Route Purpose
LoginPage /login User authentication
HomePage / Landing page
ListStockPage /products Product inventory
AddProductPage /products/add Create new product
ChangeProductDetailsPage /products/:id/edit Modify product
DeleteProductPage /products/:id/delete Remove product
SearchProductPage /search Product search interface
UserDashboard /dashboard User analytics
AdminDashboard /admin Admin management

Component Communication Patterns

Parent to Child (Props)

// Parent
<ProductList products={products} onSelectProduct={handleSelect} />

// Child
interface ProductListProps {
  products: Product[];
  onSelectProduct: (id: string) => void;
}

Child to Parent (Callbacks)

// Parent state
const [selectedId, setSelectedId] = useState<string | null>(null);

// Pass callback
<ProductItem onSelect={setSelectedId} />

// Child calls callback
const handleClick = () => props.onSelect(product.id);

Global State (Redux)

// In page component
const products = useSelector(selectProducts);
const dispatch = useDispatch();

const loadProducts = () => {
  dispatch(fetchProducts());
};

Styling Strategy

Three-Layer CSS Architecture

  1. Global Styles (src/styles/globals.css)

    • Reset, typography, base styles
  2. Component Styles (src/components/*.css and .module.css)

    • Component-specific styles
    • BEM or utility-first (Tailwind)
  3. Page Styles (src/styles/pages/*.css)

    • Page-specific layouts and styling

Best Practices

βœ… DO:

  • Use TypeScript for all components
  • Memoize components with React.memo for performance
  • Keep components focused (single responsibility)
  • Use custom hooks for logic extraction
  • Add proper prop validation with interfaces
  • Include accessibility attributes (aria-*)

❌ DON'T:

  • Prop drilling (pass through multiple levels)
  • Inline styles (use CSS/Tailwind)
  • Create components inside components
  • Use index.js as default export
  • Skip error boundaries
  • Ignore loading states

File Structure Standards

// Standard component file structure

import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import '@/styles/componentName.css';

// ===== Types =====
interface ComponentProps {
  prop1: string;
  prop2?: number;
  onChange?: (value: string) => void;
}

// ===== Component =====
export const ComponentName: React.FC<ComponentProps> = ({
  prop1,
  prop2,
  onChange
}) => {
  // Hooks
  const state = useSelector(selectState);
  const dispatch = useDispatch();

  // Event handlers
  const handleClick = () => {
    onChange?.(prop1);
  };

  // JSX
  return (
    <div className="component">
      <h2>{prop1}</h2>
      <button onClick={handleClick}>Action</button>
    </div>
  );
};

export default ComponentName;

Performance Optimization

Memoization

// Prevent unnecessary re-renders
export const ProductItem = React.memo(
  ({ product, onSelect }: Props) => (
    <div onClick={() => onSelect(product.id)}>
      {product.name}
    </div>
  ),
  (prevProps, nextProps) => {
    // Custom comparison
    return prevProps.product.id === nextProps.product.id;
  }
);

Code Splitting

// Lazy load pages
const AdminDashboard = lazy(() => import('@/pages/AdminDashboard'));

<Suspense fallback={<SkeletonLoader />}>
  <AdminDashboard />
</Suspense>

Testing Overview

All components include:

  • Unit tests for props and behavior
  • Integration tests with parent components
  • Accessibility tests (a11y)
  • Snapshot tests for UI consistency

See Component Testing for detailed patterns.



Last Updated: November 2025