Skip to content

Commit 6b2564f

Browse files
committed
♻️ Refact example app
Move observer to a useEffect hook
1 parent 504deda commit 6b2564f

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

example/App.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState} from 'react';
1+
import React, {useState, useEffect} from 'react';
22
import {
33
SafeAreaView,
44
StyleSheet,
@@ -14,6 +14,8 @@ import AppleHealthKit, {
1414
HealthKitPermissions,
1515
} from 'react-native-health';
1616

17+
import {NativeEventEmitter, NativeModules} from 'react-native';
18+
1719
/* Permission options */
1820
const permissions = {
1921
permissions: {
@@ -46,6 +48,15 @@ AppleHealthKit.initHealthKit(permissions, (error: string) => {
4648
export default function App() {
4749
const [authStatus, setAuthStatus] = useState<any>({});
4850

51+
useEffect(() => {
52+
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
53+
'healthKit:HeartRate:new',
54+
async () => {
55+
console.log('--> observer triggered');
56+
},
57+
);
58+
});
59+
4960
const handlePressGetAuthStatus = () => {
5061
AppleHealthKit.getAuthStatus(permissions, (err, result) => {
5162
if (err) {

example/index.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
* @format
33
*/
44

5-
import { AppRegistry, NativeEventEmitter, NativeModules } from 'react-native';
5+
import {AppRegistry} from 'react-native';
66
import App from './App';
7-
import { name as appName } from './app.json';
7+
import {name as appName} from './app.json';
88

9-
AppRegistry.registerComponent(appName, () => {
10-
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener('healthKit:HeartRate:new', async () => {
11-
console.log('new sample')
12-
})
13-
return App
14-
});
9+
AppRegistry.registerComponent(appName, () => App);

0 commit comments

Comments
 (0)