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

Doesn't work with new bridgeless architecture on 0.76.0 #395

Open
andrewzey opened this issue Oct 30, 2024 · 1 comment · May be fixed by #396
Open

Doesn't work with new bridgeless architecture on 0.76.0 #395

andrewzey opened this issue Oct 30, 2024 · 1 comment · May be fixed by #396
Labels
bug Something isn't working

Comments

@andrewzey
Copy link

andrewzey commented Oct 30, 2024

Describe the bug
Because the new react-native architecture lazy loads modules by default, require('react-native').NativeModules is an empty object at app startup time. Search for "Lazy module loading by default" in https://reactnative.dev/blog/2024/10/23/the-new-architecture-is-here

To Reproduce
Load the app on React Native 0.76.0 and observe that all functions expected on AppleHealthKit are undefined.

Expected behavior
All functions on AppleHealthKit are should be defined at access time.

Workaround Confirmed to Work
Per this user's helpful comment (facebook/react-native#29228 (comment)), this workaround fixes the issue for MY app which only uses a handful of functions from react-native-health. This is far from a global solution though. I'm using yarn so I'm able to simply do a yarn patch react-native-health to be unblocked with this change:

index.js

import { Activities, Observers, Permissions, Units } from './src/constants'

// const { AppleHealthKit } = require('react-native').NativeModules

// export const HealthKit = Object.assign({}, AppleHealthKit, {
//   Constants: {
//     Activities,
//     Observers,
//     Permissions,
//     Units,
//   },
// })

// Temporary workaround per:
// https://github.com/facebook/react-native/issues/29228#issuecomment-2345062090
const NativeModules = require('react-native').NativeModules
export const HealthKit = {
  isAvailable: NativeModules.AppleHealthKit.isAvailable,
  initHealthKit: NativeModules.AppleHealthKit.initHealthKit,
  getDailyStepCountSamples: NativeModules.AppleHealthKit.getDailyStepCountSamples,
  getSamples: NativeModules.AppleHealthKit.getSamples,

  Constants: {
    Activities,
    Observers,
    Permissions,
    Units,
  },
}

module.exports = HealthKit
@andrewzey andrewzey added the bug Something isn't working label Oct 30, 2024
@andrewzey
Copy link
Author

andrewzey commented Oct 30, 2024

An example of the error message a user would encounter is:

_reactNativeHealth.default.isAvailable is not a function (it is undefined)

Simulator Screenshot - iPhone 16 Pro - 2024-10-29 at 20 58 17

@andrewzey andrewzey linked a pull request Oct 30, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant