Skip to content

Instantly share code, notes, and snippets.

@codinronan
Forked from EduVencovsky/useDimensions.js
Created July 18, 2019 03:22
Show Gist options
  • Save codinronan/3adb283b7be1363fd45161fe403decb9 to your computer and use it in GitHub Desktop.
Save codinronan/3adb283b7be1363fd45161fe403decb9 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