1
+ import { isEqual } from 'lodash' ;
1
2
import { useCallback , useRef , useState , RefCallback } from 'react' ;
2
- import { View } from 'react-native' ;
3
+ import { View , LayoutChangeEvent , LayoutRectangle } from 'react-native' ;
3
4
import { Constants } from '../../commons/new' ;
4
5
import { PanningDirectionsEnum } from '../panView' ;
5
6
@@ -28,22 +29,20 @@ export default function useHiddenLocation<T extends View>() {
28
29
29
30
const [ hiddenLocation , setHiddenLocation ] = useState < HiddenLocation > ( getHiddenLocation ( { } ) ) ;
30
31
const ref = useRef < T > ( ) ;
32
+ const layoutData = useRef < LayoutRectangle > ( ) ;
31
33
const wasMeasured = useRef ( false ) ;
32
34
33
35
const measure = useCallback ( ( ) => {
34
- if ( ! wasMeasured . current ) {
35
- ref . current ?. measureInWindow ( ( x , y , width , height ) => {
36
- if ( ! wasMeasured . current && width > 0 && height > 0 ) {
37
- wasMeasured . current = true ;
38
- setHiddenLocation ( getHiddenLocation ( {
39
- x,
40
- y,
41
- width,
42
- height,
43
- wasMeasured : true
44
- } ) ) ;
45
- }
46
- } ) ;
36
+ if ( ref . current && layoutData . current && layoutData . current . width > 0 && layoutData . current . height > 0 ) {
37
+ wasMeasured . current = true ;
38
+ const { x, y, width, height} = layoutData . current ;
39
+ setHiddenLocation ( getHiddenLocation ( {
40
+ x,
41
+ y,
42
+ width,
43
+ height,
44
+ wasMeasured : true
45
+ } ) ) ;
47
46
}
48
47
} , [ ] ) ;
49
48
@@ -55,9 +54,13 @@ export default function useHiddenLocation<T extends View>() {
55
54
} ,
56
55
[ measure ] ) ;
57
56
58
- const onLayout = useCallback ( ( ) => {
59
- measure ( ) ;
60
- } , [ measure ] ) ;
57
+ const onLayout = useCallback ( ( event : LayoutChangeEvent ) => {
58
+ if ( ! isEqual ( layoutData . current , event . nativeEvent . layout ) ) {
59
+ layoutData . current = event . nativeEvent . layout ;
60
+ measure ( ) ;
61
+ }
62
+ } ,
63
+ [ measure ] ) ;
61
64
62
65
return { setRef, onLayout, hiddenLocation} ;
63
66
}
0 commit comments