首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dimensions.get('screen')不工作于React本机

Dimensions.get('screen')不工作于React本机
EN

Stack Overflow用户
提问于 2021-04-10 04:10:31
回答 1查看 1K关注 0票数 2

我使用反应-本机维度来获取屏幕的尺寸,并确定用户是在纵向还是横向,mode.....however Dimensions.get('screen').height总是作为两面中较长的一方回来,即使在景观模式下也是如此。Dimensions.get('screen').height总是比Dimensions.get('screen').width大,即使在景观模式下也是如此。

代码语言:javascript
复制
constructor(props) {
    super(props);
    this.state = {
      orientation: (Dimensions.get('screen').height >= Dimensions.get('screen').width ? 'portrait' : 'landscape')
    };

    Dimensions.addEventListener('change', () => {
      let orientation = (Dimensions.get('screen').height >= Dimensions.get('screen').width ? 'portrait' : 'landscape');
      this.setState({
        orientation: orientation
      });
    });
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-10 09:34:29

使用useWindowDimensions()。只需将类似的内容放在根组件中:

代码语言:javascript
复制
const SCREEN_WIDTH = useWindowDimensions().width;
const SCREEN_HEIGHT = useWindowDimensions().height;
return (
    <View style={{ width: SCREEN_WIDTH, minHeight: SCREEN_HEIGHT}} >
        //the rest of your app
    </View>
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67030717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档