Overview
MotorDesk uses custom React hooks to encapsulate business logic, state management, and side effects. This approach keeps components clean and makes logic reusable across the application.Available Hooks
All custom hooks are located insrc/hooks/:
Hook Categories
Authentication
useAuthUser login, registration, session managementData Management
useCustomers, useVehicles, useProducts, useSalesCRUD operations for business entitiesUI State
useMainLayoutLayout state, modals, navigationBusiness Logic
usePermissions, useHomeStats, useReportsComplex business rules and calculationsConfiguration
useSettingsApp configuration and preferencesuseAuth Hook
Handles authentication, user session, and onboarding. Location:src/hooks/useAuth.ts
Return Values
Usage Example
Key Features
Login Simulation
Login Simulation
Simulates authentication against mock user data:
User Registration
User Registration
Creates a new owner account (first-time setup):
Redux Integration
Redux Integration
Uses Redux for global auth state:
useCustomers Hook
Manages customer data with search, pagination, and modal state. Location:src/hooks/useCustomers.ts
Return Values
Usage Example
Key Features
Data Enrichment with Vehicle Counts
Data Enrichment with Vehicle Counts
Automatically calculates related vehicle counts:
Real-time Search Filtering
Real-time Search Filtering
Filters customers as user types:
Pagination Logic
Pagination Logic
Calculates current page items:
External API Integration (Decolecta)
External API Integration (Decolecta)
Validates RUC/DNI against SUNAT/RENIEC:
Common Hook Patterns
Modal State Management
All entity management hooks follow this pattern:Search and Filter Pattern
Pagination Pattern
Best Practices
Single Responsibility
Each hook should have one clear purpose. Don’t create “god hooks” that do everything.
Memoization
Use
useMemo for expensive calculations. Use useCallback for function references passed as props.Dependency Arrays
Always specify correct dependencies in
useEffect, useMemo, and useCallback.Return Object
Return an object (not array) for hooks with multiple values. This allows selective destructuring.
Type Safety
Always define TypeScript interfaces for hook parameters and return values.
Naming Convention
Always prefix with
use (e.g., useAuth, useCustomers). This is required by React’s rules.Hook Dependencies
Redux Integration
Many hooks integrate with Redux for global state:Local Database (Mock)
Hooks access the mock database for CRUD operations:Testing Hooks
Use React Testing Library’srenderHook to test custom hooks:
Performance Optimization
Hook Composition
Hooks can use other hooks:Next Steps
- Learn about Component Architecture that uses these hooks
- Review Project Structure for code organization
- Explore detailed hook APIs in the API Reference
