Introduction
Proton WebClients provides a comprehensive collection of React hooks that handle common patterns across the application suite. These hooks are designed to be composable, type-safe, and efficient.Hook Categories
State Management Hooks
Hooks for managing component state with advanced patterns:- useLoading - Track async operation loading states
- useControlled - Handle controlled/uncontrolled component patterns
- useStateRef - Combine state with ref for sync access
- usePrevious - Access previous value of state
Data Fetching Hooks
Hooks for API interactions and data fetching:- useApi - Access the API client
- useApiResult - Fetch data with loading and error states
- useCache - Access the application cache
Lifecycle Hooks
Hooks for component lifecycle management:- useIsMounted - Check if component is mounted
- useEffectOnce - Run effect only once on mount
- useInterval - Declarative interval with cleanup
Utility Hooks
Hooks for common React patterns:- useCombinedRefs - Combine multiple refs into one
- useInstance - Create stable instance value
- useDateCountdown - Countdown timer to a date
- useSearchParams - Read and write URL search parameters
- useAsyncError - Throw errors asynchronously for Error Boundaries
- useStableLoading - Prevent loading state flickering
- useSynchronizingState - State that syncs with prop value
Package Structure
Hooks are organized into two main packages:Usage Example
TypeScript Support
All hooks are fully typed with TypeScript and provide excellent type inference:Best Practices
1. Use the Right Hook for the Job
Choose hooks that match your specific use case:- Use
useLoadingfor single operations - Use
useLoadingByKeywhen tracking multiple concurrent operations - Use
useApiResultfor data fetching with automatic loading states
2. Handle Cleanup Properly
Many hooks handle cleanup automatically, but be aware of unmount scenarios:3. Combine Hooks Effectively
Next Steps
- Learn about State Management Hooks
- Explore Data Fetching Hooks
- Check out Common Types