UNPKG

668 BTypeScriptView Raw
1import * as React from 'react';
2import type { LayoutChangeEvent } from 'react-native';
3
4export default function useLayout() {
5 const [layout, setLayout] = React.useState<{
6 height: number;
7 width: number;
8 measured: boolean;
9 }>({ height: 0, width: 0, measured: false });
10
11 const onLayout = React.useCallback(
12 (e: LayoutChangeEvent) => {
13 const { height, width } = e.nativeEvent.layout;
14
15 if (height === layout.height && width === layout.width) {
16 return;
17 }
18
19 setLayout({
20 height,
21 width,
22 measured: true,
23 });
24 },
25 [layout.height, layout.width]
26 );
27
28 return [layout, onLayout] as const;
29}