Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/kingstinct/react-native-healthkit/llms.txt

Use this file to discover all available pages before exploring further.

Automatic subscription to quantity sample changes with React lifecycle management.

Hook signature

function useSubscribeToQuantitySamples<TIdentifier extends QuantityTypeIdentifier>(
  identifier: TIdentifier,
  onChange: (args: OnQuantitySamplesCallback) => void
): void
identifier
QuantityTypeIdentifier
required
The quantity type to subscribe to (e.g., 'HKQuantityTypeIdentifierStepCount')
onChange
OnQuantitySamplesCallback
required
Callback function invoked when new quantity samples are available

Usage

Subscribe to step count changes

import { useSubscribeToQuantitySamples } from '@kingstinct/react-native-healthkit';

function StepCounter() {
  const [steps, setSteps] = useState<number>(0);

  useSubscribeToQuantitySamples(
    'HKQuantityTypeIdentifierStepCount',
    ({ samples }) => {
      const totalSteps = samples.reduce((sum, sample) => sum + sample.quantity, 0);
      setSteps(prev => prev + totalSteps);
    }
  );

  return <Text>Total Steps: {steps}</Text>;
}

Subscribe to heart rate with processing

import { useSubscribeToQuantitySamples } from '@kingstinct/react-native-healthkit';

function HeartRateMonitor() {
  const [heartRates, setHeartRates] = useState<number[]>([]);

  useSubscribeToQuantitySamples(
    'HKQuantityTypeIdentifierHeartRate',
    ({ samples }) => {
      const newRates = samples.map(s => s.quantity);
      setHeartRates(prev => [...prev, ...newRates].slice(-100)); // Keep last 100
    }
  );

  const averageHR = heartRates.length > 0
    ? heartRates.reduce((a, b) => a + b, 0) / heartRates.length
    : 0;

  return <Text>Average HR: {averageHR.toFixed(0)} bpm</Text>;
}

Features

  • Automatic cleanup - Subscription is removed when component unmounts
  • Stable callback - Uses ref to avoid re-subscribing on callback changes
  • Type-safe - Full TypeScript support with inferred sample types
  • Background updates - Works with background delivery when enabled

Notes

Request authorization before using this hook, otherwise your app will crash.
Enable background delivery for this data type to receive updates when your app isn’t in the foreground.

Build docs developers (and LLMs) love