Last active
June 2, 2021 15:51
React Native HOC (Higher Order Component) that adds 'orientation' and 'isLandscape' props to the component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import { Dimensions } from 'react-native'; | |
const getOrientation = () => Dimensions.get('window').width < Dimensions.get('window').height ? 'portrait' : 'landscape'; | |
export default function withOrientation(WrappedComponent) { | |
return function (props) { | |
const [orientation, setOrientation] = useState(getOrientation()); | |
useEffect(() => { | |
Dimensions.addEventListener('change', () => setOrientation(getOrientation())); | |
return () => Dimensions.removeEventListener('change'); | |
}, []) | |
return <WrappedComponent {...props} orientation={orientation} isLandscape={orientation === 'landscape'} />; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment