Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dws-krisha opened this issue Nov 19, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@dws-krisha
Copy link

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 && (

)}

);
};

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?

@dws-krisha dws-krisha added the enhancement New feature or request label Nov 19, 2024
@fontesrp
Copy link

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

@kapilw360
Copy link

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

Same problem im facing. im using native 0.76.3. version. so you solved this issue ? or the problem is with react-native-health dependency only. please let me know .

@florisjkruger4
Copy link

@kapilw360 - I was able to fix it my looking at the file changes PR:
Link

You will need to make your index.js file in:
node_modules > react0native-health > index.js

look like the index.js in here.
Hope this helps, good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants