Created
August 7, 2020 07:54
-
-
Save Krisztiaan/0c186b08a7342078f0ecb545eb547ff5 to your computer and use it in GitHub Desktop.
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, useCallback } from 'react' | |
import { LayoutChangeEvent } from 'react-native' | |
interface Measurement { | |
width: number | |
height: number | |
} | |
type OnLayoutCallback = (event: LayoutChangeEvent) => void | |
/** | |
* Returns a `{ width: number, height: number }` or `undefined` and an `onLayout`. | |
* | |
* Use the second item as the `onLayout` prop on the view you want to measure. | |
*/ | |
export default function useComponentSize() { | |
const [size, setSize] = useState<Measurement>() | |
const onLayout = useCallback<OnLayoutCallback>((event) => { | |
const { width, height } = event.nativeEvent.layout | |
setSize({ width, height }) | |
}, []) | |
return [size, onLayout] as const | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment