GrainProvider
Context provider for Grain Analytics in React apps.config- Configuration object (provider-managed pattern)client- External client instance (external client pattern)children- React children
config or client, not both.
useConfig()
Get a single configuration value with automatic updates.key- Configuration keyoptions.properties- User properties for personalizationoptions.forceRefresh- Skip cache, fetch from APIoptions.immediateKeys- Keys to fetch immediately
useAllConfigs()
Get all configurations with automatic updates.options.properties- User properties for personalizationoptions.forceRefresh- Skip cache, fetch from API
useTrack()
Get a stable track function for event tracking.- Stable reference (won’t cause re-renders)
- No need for
useCallback - Safe to pass to child components
useGrainAnalytics()
Get the full Grain client instance.- User identification
- User properties
- Template events
- Manual flushing
- Advanced configuration
Hook Options Types
UseConfigOptions
UseAllConfigsOptions
Hook Return Types
UseConfigResult
UseAllConfigsResult
Error Handling
Hooks provide error states for graceful degradation:Performance Notes
useConfigonly re-renders when specific config changesuseAllConfigsre-renders when any config changesuseTrackreturns stable function (no re-renders)- Hooks are memoized internally for optimal performance