Skip to content

Instantly share code, notes, and snippets.

@klippx
Last active November 12, 2019 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klippx/aa1f4e9546c26f5d649743be90fa3e6a to your computer and use it in GitHub Desktop.
Save klippx/aa1f4e9546c26f5d649743be90fa3e6a to your computer and use it in GitHub Desktop.
useWindowSize [native]
import { useState, useEffect } from 'react'
import { Dimensions } from 'react-native'
const useWindowSize = () => {
const { width, height } = Dimensions.get('window')
const [state, setState] = useState({ width, height })
useEffect(() => {
const handler = result => {
setState({
width: result.window.width,
height: result.window.height,
})
}
Dimensions.addEventListener('change', handler)
return () => Dimensions.removeEventListener('change', handler)
})
return state
}
export default useWindowSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment