m3k_1
m3k_1

Reputation: 417

JavaScript function that detects phone orientation change

Long story short I'm writing a calculator app in react-native and i want to have 2 different views for when the phone is vertiacly and horizontaly, is there a quick way to get the phone orientation without installing any packages

Upvotes: 0

Views: 89

Answers (1)

Hagai Harari
Hagai Harari

Reputation: 2877

import { Dimensions } from 'react-native';

function useIsHorizonal() {
  const [isHorizonal, setIsHorizonal] = useState( Dimensions.screen.width > Dimensions.screen.height )
  useEffect(()=> {
     Dimensions.addEventListener(
         'change',
         ({ screen: { width, height }}) =>
              setIsHorizonal(width > height)
     )
   }, [])
   return isHorizonal
}

Upvotes: 2

Related Questions