_reactNativeHealth.default.initHealthKit is not a function (it is undefined) 报错

我遇到了React Native HealthKit初始化报错“_reactNativeHealth.default.initHealthKit is not a function (it is undefined)”的问题,发现是因为RN新版本的模块懒加载机制导致NativeModules为空对象。我通过手动获取所需函数并赋值给Kit对象,临时解决了问题,以便继续开发。

App开发
// https://www.npmjs.com/package/vanilla-lazyload // Set the options globally // to make LazyLoad self-initialize window.lazyLoadOptions = { elements_selector: ".lazy", }; // Listen to the initialization event // and get the instance of LazyLoad window.addEventListener( "LazyLoad::Initialized", function (event) { window.lazyLoadInstance = event.detail.instance; }, false ); document.addEventListener('DOMContentLoaded', function () { window.lazyLoadInstance?.update(); });