Overview
DPM Delivery Mobile uses TanStack Query (React Query) for server state management. This provides automatic caching, background refetching, and optimistic updates.Setup
The QueryClient is configured in the Providers component:- retry: 2 attempts on failure
- Queries are cached and refetched in background
- Stale time and cache time use defaults
Query Keys
Centralized query keys ensure consistency and avoid cache collisions:- Hierarchical organization by domain
- Parameters included for cache invalidation
- Consistent naming convention
Query Options
Query options are defined in separate files for reusability:Shipment Queries
User Queries
Using Queries in Components
Basic Query
Query with Parameters
Dependent Queries
Mutations
Mutations handle data updates with automatic cache invalidation:Using Mutations in Components
Custom API Hooks
Complex API interactions can be wrapped in custom hooks:Caching Strategies
Automatic Background Refetch
Queries automatically refetch when:- Component mounts (if stale)
- Window regains focus
- Network reconnects
Manual Invalidation
Optimistic Updates
Prefetching
Query Configuration Options
Best Practices
- Centralize query keys in
query-keys.tsto avoid typos and maintain consistency - Create query options for reusable queries
- Use mutations for data updates with automatic invalidation
- Implement optimistic updates for better UX on slow connections
- Handle loading and error states explicitly in components
- Prefetch data before navigation for instant loading
- Use enabled option for dependent queries
- Invalidate related queries after mutations