-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (28 loc) · 853 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { useEffect, useState } from 'react';
import { Dimensions } from 'react-native';
import { accelerometer } from 'react-native-sensors';
const useDeviceOrientation = () => {
const [orientation, setOrientation] = useState('portrait');
useEffect(() => {
const subscription = accelerometer.subscribe(({ x }) => {
if (Math.abs(x) > 5) {
setOrientation('landscape');
} else {
setOrientation('portrait');
}
});
Dimensions.addEventListener('change', ({ window: { width, height } }) => {
if (width < height) {
setOrientation('portrait');
} else {
setOrientation('landscape');
}
});
return () => {
subscription.unsubscribe();
Dimensions.removeEventListener('change');
};
}, []);
return orientation;
};
export default useDeviceOrientation;