Skip to content

Commit 18207b9

Browse files
workwork
work
authored and
work
committed
add hooks implementation to README
1 parent f930d3e commit 18207b9

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public class MainApplication extends Application implements ReactApplication {
9393
4. Add `NSMotionUsageDescription` key to your `Info.plist` with strings describing why your app needs this permission
9494

9595

96-
## Usage
96+
## Usage
97+
### Class based implementation
9798

9899
```js
99100
import ActivityRecognition from 'react-native-activity-recognition'
@@ -118,6 +119,35 @@ ActivityRecognition.stop()
118119
this.unsubscribe()
119120
```
120121

122+
### Hooks based implementation
123+
124+
```js
125+
import ActivityRecognition from 'react-native-activity-recognition'
126+
127+
...
128+
129+
// Subscribe to updates on mount
130+
131+
useEffect(() => {
132+
ActivityRecognition.subscribe(detectedActivities => {
133+
const mostProbableActivity = detectedActivities.sorted[0];
134+
console.log(mostProbableActivity);
135+
});
136+
137+
// Stop activity detection and remove the listener on unmount
138+
139+
return ActivityRecognition.stop();
140+
});
141+
142+
...
143+
144+
// Start activity detection
145+
146+
const detectionIntervalMillis = 1000
147+
ActivityRecognition.start(detectionIntervalMillis)
148+
149+
```
150+
121151
### Android
122152

123153
`detectedActivities` is an object with keys for each detected activity, each of which have an integer percentage (0-100) indicating the likelihood that the user is performing this activity. For example:

0 commit comments

Comments
 (0)