Skip to main content

GrainProvider

Context provider for Grain Analytics in React apps.
Props:
  • config - Configuration object (provider-managed pattern)
  • client - External client instance (external client pattern)
  • children - React children
Example:
Note: Provide either config or client, not both.

useConfig()

Get a single configuration value with automatic updates.
Parameters:
  • key - Configuration key
  • options.properties - User properties for personalization
  • options.forceRefresh - Skip cache, fetch from API
  • options.immediateKeys - Keys to fetch immediately
Returns:
Example:

useAllConfigs()

Get all configurations with automatic updates.
Parameters:
  • options.properties - User properties for personalization
  • options.forceRefresh - Skip cache, fetch from API
Returns:
Example:

useTrack()

Get a stable track function for event tracking.
Returns: Stable track function (never changes between renders) Track Function Signature:
Example:
Benefits:
  • Stable reference (won’t cause re-renders)
  • No need for useCallback
  • Safe to pass to child components

useGrainAnalytics()

Get the full Grain client instance.
Returns: Grain client instance Example:
Use when you need:
  • 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

  • useConfig only re-renders when specific config changes
  • useAllConfigs re-renders when any config changes
  • useTrack returns stable function (no re-renders)
  • Hooks are memoized internally for optimal performance

Next Steps

React Overview

Learn React integration

React Examples

See complete examples