Lesson 2 of 8
Feature-Based Architecture
Organizing code by feature (vertical slices) rather than by type (components, hooks, utils) scales much better as applications grow. Each feature folder contains everything it needs - its components, hooks, API calls, types, and tests. Moving or deleting a feature means removing one directory.
Feature-Based Folder Structure
// ❌ Type-based structure - doesn't scale
src/
components/
UserCard.tsx ← depends on types in models/
ProductList.tsx ← depends on api/ and store/
OrderSummary.tsx ← depends on all three
hooks/
useUser.ts
useProducts.ts
api/
userApi.ts
productApi.ts
models/
user.types.ts
product.types.ts
store/
userSlice.ts
productSlice.ts
// Problem: Every cross-cutting concern spans 6 directories
// Deleting "users" feature requires hunting across all dirs
// ✅ Feature-based structure - scalable
src/
features/
auth/
components/
LoginForm.tsx
SignupForm.tsx
AuthProvider.tsx
hooks/
useAuth.ts
useAuthForm.ts
api/
authApi.ts
types.ts
index.ts // public API - only export what other features need
products/
components/
ProductCard.tsx
ProductList.tsx
ProductFilters.tsx
hooks/
useProducts.ts
useProductFilter.ts
api/
productApi.ts
store/
productSlice.ts
types.ts
index.ts
checkout/
components/
CheckoutForm.tsx
OrderSummary.tsx
PaymentStep.tsx
hooks/
useCheckout.ts
api/
orderApi.ts
types.ts
index.ts
shared/ // truly shared across multiple features
components/
Button.tsx
Input.tsx
Modal.tsx
hooks/
useDebounce.ts
useMediaQuery.ts
utils/
formatters.ts
validators.ts
types/
common.types.ts
app/
App.tsx
routes.tsx
providers.tsx
// index.ts - each feature exports only its public interface
// features/products/index.ts
export { ProductList } from './components/ProductList';
export { ProductCard } from './components/ProductCard';
export { useProducts } from './hooks/useProducts';
export type { Product, ProductFilters } from './types';
// Don't export internals like productSlice reducer
// Import between features - only via index.ts (barrel file)
import { ProductList } from '@/features/products'; // ✅
import { productSlice } from '@/features/products/store/productSlice'; // ❌ implementation detailTip
Tip
Practice FeatureBased Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Feature-Based Architecture
Practice Task
Note
Practice Task - (1) Write a working example of FeatureBased Architecture from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with FeatureBased Architecture is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?