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
Global Styles (
src/styles/globals.css)- Reset, typography, base styles
Component Styles (
src/components/*.cssand.module.css)- Component-specific styles
- BEM or utility-first (Tailwind)
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.
Related Documentation
- Shared Components Details - Header, Sidebar, Footer, Buttons, etc.
- Page Components - Page-level components and routes
- Styling - CSS organization and Tailwind usage
- Testing Components - Testing patterns and examples
- Pages Architecture - Page structure and lifecycle
- Services - Data layer integration
Last Updated: November 2025