-
-
Save codinronan/3adb283b7be1363fd45161fe403decb9 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