Screen width in React Native

前端 未结 10 960
独厮守ぢ
独厮守ぢ 2020-12-13 08:33

How do I get screen width in React native?

(I need it because I use some absolute components that overlap and their position on screen changes with different device

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 09:01

    April 10th 2020 Answer:

    The suggested answer using Dimensions is now discouraged. See: https://reactnative.dev/docs/dimensions

    The recommended approach is using the useWindowDimensions hook in React; https://reactnative.dev/docs/usewindowdimensions which uses a hook based API and will also update your value when the screen value changes (on screen rotation for example):

    import {useWindowDimensions} from 'react-native';
    
    const windowWidth = useWindowDimensions().width;
    const windowHeight = useWindowDimensions().height;
    

    Note: useWindowDimensions is only available from React Native 0.61.0: https://reactnative.dev/blog/2019/09/18/version-0.61

提交回复
热议问题