Lesson 6 of 10
Feature-Based Architecture - Vertical Slices
Feature-based (vertical slice) architecture organizes code by what it does, not by technical layer. All auth code lives in src/features/auth/ instead of scattered across components/, screens/, services/, stores/. This makes features independently shippable and easy to delete.
Vertical Slice Structure
// src/features/auth/
// ├── components/
// │ ├── LoginForm.tsx ← UI only, no business logic
// │ ├── PasswordStrengthBar.tsx
// │ └── index.ts ← barrel: export { LoginForm }
// ├── hooks/
// │ ├── useSignIn.ts ← connects component to service
// │ ├── useSignUp.ts
// │ └── index.ts
// ├── screens/
// │ ├── LoginScreen.tsx ← navigation-aware screen
// │ ├── SignUpScreen.tsx
// │ └── index.ts
// ├── services/
// │ ├── authService.ts ← API calls (no UI)
// │ └── tokenService.ts ← SecureStore w/r/d operations
// ├── store/
// │ └── authStore.ts ← Zustand or Context
// ├── types.ts ← interfaces: User, AuthState, SignInDto
// └── index.ts ← public API for other features
// ---- Barrel file (index.ts) pattern ----
// Defines exactly what this feature exposes to other features
// src/features/auth/index.ts:
export { LoginForm } from './components';
export { SignUpScreen, LoginScreen } from './screens';
export { useSignIn, useSignUp } from './hooks';
export type { User, AuthState } from './types';
// NOT exported: internal helpers, implementation details
// ---- Cross-feature imports ----
// ✅ Feature imports from shared/
import { Button } from '@shared/components';
import { apiClient } from '@shared/services';
// ✅ Feature imports from another feature's public API only
import type { User } from '@features/auth'; // type import from barrel
// ❌ Never: deep import into another feature's internals
import { authReducer } from '@features/auth/store/authStore'; // WRONGShared Module Structure
// src/shared/ - code used by 2+ features
// ├── components/
// │ ├── Button.tsx ← generic, no business logic
// │ ├── Card.tsx
// │ ├── EmptyState.tsx
// │ ├── LoadingOverlay.tsx
// │ ├── ErrorBoundary.tsx
// │ └── index.ts
// ├── hooks/
// │ ├── useDebounce.ts
// │ ├── useNetworkStatus.ts
// │ ├── usePrevious.ts
// │ └── index.ts
// ├── services/
// │ ├── apiClient.ts ← axios instance w/ interceptors
// │ ├── storageService.ts ← AsyncStorage wrapper
// │ └── index.ts
// ├── utils/
// │ ├── formatDate.ts
// │ ├── validators.ts
// │ ├── currency.ts
// │ └── index.ts
// ├── types/
// │ ├── api.ts ← PaginatedResponse, ApiError
// │ └── index.ts
// └── constants/
// ├── theme.ts ← COLORS, SPACING, TYPOGRAPHY
// ├── breakpoints.ts
// └── index.ts
// PATH ALIASES (tsconfig.json + babel.config.js)
// "@features/*" → "src/features/*"
// "@shared/*" → "src/shared/*"
// "@constants/*"→ "src/constants/*"
// Result: no ../../../ relative hell
import { Button } from '@shared/components';
import { COLORS } from '@constants/theme';
import type { User } from '@features/auth';Tip
Tip
Practice FeatureBased Architecture Vertical Slices in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Organizing scalable React Native codebases by self-contained domain features
Practice Task
Note
Practice Task - (1) Write a working example of FeatureBased Architecture Vertical Slices 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 Vertical Slices is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?