Skip to content

Instantly share code, notes, and snippets.

@EduVencovsky
Created July 13, 2019 13:56
Show Gist options
  • Save EduVencovsky/67e17133d5488745bb56af540c9a2e5c to your computer and use it in GitHub Desktop.
Save EduVencovsky/67e17133d5488745bb56af540c9a2e5c to your computer and use it in GitHub Desktop.
React Native Hook for Dimensions
import { useState, useEffect } from 'react'
import { Dimensions } from 'react-native'
const useDimensions = getter => {
const [dimensions, setDimensions] = useState(Dimensions.get(getter))
useEffect(() => {
const widthHandler = d => setDimensions(d[getter])
Dimensions.addEventListener('change', widthHandler)
return () => Dimensions.removeEventListener('change', widthHandler)
})
return dimensions
}
export default useDimensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment