Created
July 13, 2019 13:56
-
-
Save EduVencovsky/67e17133d5488745bb56af540c9a2e5c to your computer and use it in GitHub Desktop.
React Native Hook for Dimensions
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 { 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