agencyenterprise / react-native-health

A React Native package to interact with Apple HealthKit
MIT License
894 stars 238 forks source link

Warning: TypeError: _reactNativeHealth.default.initHealthKit is not a function (it is undefined) #399

Open dws-krisha opened 1 week ago

dws-krisha commented 1 week ago

import React, { useEffect, useState } from "react"; import { View, Text, Button, Alert } from "react-native"; import AppleHealthKit, { HealthKitPermissions } from "react-native-health";

const App = () => { const [isHealthKitAvailable, setIsHealthKitAvailable] = useState(false); const [hasPermissions, setHasPermission] = useState(false); const [steps, setSteps] = useState<number | null>(null);

const permissions: HealthKitPermissions = { permissions: { read: [AppleHealthKit.Constants.Permissions.Steps], write: [], }, };

const fetchSteps = () => { const options = { startDate: new Date(2024, 0, 1).toISOString(), // Example: fetch data from Jan 1, 2024 endDate: new Date().toISOString(), };

AppleHealthKit.getStepCount(options, (err, results) => {
  if (err) {
    console.error("Error fetching step count:", err);
    return;
  }
  setSteps(results.value);
});

};

useEffect(() => { // Check if HealthKit is available AppleHealthKit?.isAvailable((err, available) => { if (err || !available) { Alert.alert( "HealthKit Not Available", "This device does not support Apple HealthKit." ); return; } setIsHealthKitAvailable(true);

  // Initialize HealthKit
  AppleHealthKit?.initHealthKit(permissions, (err) => {
    if (err) {
      console.log("Error getting permissions");
      return;
    }
    setHasPermission(true);
    fetchSteps();
  });
});

}, []);

return ( <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>

{isHealthKitAvailable ? `Steps Count: ${steps ?? "Loading..."}` : "HealthKit not available"}
  {isHealthKitAvailable && (
    <Button title="Refresh Steps" onPress={fetchSteps} />
  )}
</View>

); };

export default App;

I dont know what am i missing here my library is installed properly ,pods are installed correctly but getting such error. same error also there for isAvailable call. Can anyone suggest or help me out to solve this issue?

fontesrp commented 1 week ago

This started happening in our project when we upgraded to React Native 0.76. The library works well with React Native 0.75.4.

This issue has already been reported here, and this PR has a fix