react-native - React Native,使用纵向模式检测屏幕旋转变化

标签 react-native react-native-android react-native-ios

我在 react-native 应用程序中使用纵向模式。但我想捕获屏幕的旋转事件。
有没有办法做到这一点?

谢谢...

最佳答案

这个功能可能对你有帮助
创建 useOrientation.js文件

import {useEffect, useState} from 'react';
import {Dimensions} from 'react-native';

export function useOrientation(){
  const [orientation, setOrientation] = useState("PORTRAIT");

  useEffect(() => {
    Dimensions.addEventListener('change', ({window:{width,height}})=>{
      if (width<height) {
        setOrientation("PORTRAIT")
      } else {
        setOrientation("LANDSCAPE")
    
      }
    })

  }, []);

  return orientation;
}

关于react-native - React Native,使用纵向模式检测屏幕旋转变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62472029/

相关文章:

react-native - 显示键盘 react 原生时隐藏底部 ImageView

javascript - React Fragment 不适用于输入组件

react-native-paytm 不工作

xcode - react native : xcode build success on device but doesn't connect to metro builder

react-native - 处理命令时遇到错误(域=NSPOSIXErrorDomain,代码=2)

react-native - 如何修复导入错误 : 'requireNativeComponent' from 'react-native-web'

android - React Native 垂直交付进度条

node.js - npx react-native init AwesomeProject 错误 enolocal

react-native - 在设备上运行 React Native Android

ios - 监视文件更改时出错 : EMFILE {"code" :"EMFILE" ,"errno" :"EMFILE" ,"syscall" :"Error watching file for changes:" ,"filename": null}?