React native 方向更改侦听器是否未启动?
我想在expo react native中检测设备的当前方向,这是我无法工作的代码:React native 方向更改侦听器是否未启动?,react-native,expo,React Native,Expo,我想在expo react native中检测设备的当前方向,这是我无法工作的代码: import { Dimensions, } from 'react-native'; import * as ScreenOrientation from 'expo-screen-orientation';** const App = () => { ... useEffect(() => { const isPortrait = () => { const dime
import {
Dimensions,
} from 'react-native';
import * as ScreenOrientation from 'expo-screen-orientation';**
const App = () => {
...
useEffect(() => {
const isPortrait = () => {
const dimension = Dimensions.get('screen');
return dimension.height >= dimension.width;
};
Dimensions.addEventListener('change', () => {
const orientation = isPortrait() ? 'portrait' : 'landscape';
console.log('Dimensions orientation', orientation);
});
ScreenOrientation.addOrientationChangeListener((e) => {
console.log('e ', e);
});
}, []);
当我旋转设备时,没有日志,因此它不会启动?这对我很有用:
const [orientation, setOrientation] = useState(ScreenOrientation.Orientation.PORTRAIT_UP);
useEffect(()=>{
// set initial orientation
ScreenOrientation.getOrientationAsync()
.then((info) =>{
setOrientation(info.orientation);
});
// subscribe to future changes
const subscription = ScreenOrientation.addOrientationChangeListener((evt)=>{
setOrientation(evt.orientationInfo.orientation);
});
// return a clean up function to unsubscribe from notifications
return ()=>{
ScreenOrientation.removeOrientationChangeListener(subscription);
}
}, []);