-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdebug-react.tsx
More file actions
72 lines (65 loc) · 1.7 KB
/
debug-react.tsx
File metadata and controls
72 lines (65 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as React from 'react'
import {type LayoutChangeEvent, View, Pressable, Text} from 'react-native'
import {debugClear} from './debug'
const ENABLE_UNMOUNT_ALL = __DEV__ && (false as boolean)
const UnmountAll = ({setShow}: {setShow: React.Dispatch<React.SetStateAction<boolean>>}) => {
return (
<View
style={{
backgroundColor: 'red',
height: 40,
left: 0,
position: 'absolute',
top: 20,
width: 100,
zIndex: 999,
}}
>
<Pressable
onPress={() => {
setShow(s => !s)
}}
>
<Text>Swap All</Text>
</Pressable>
</View>
)
}
export const useUnmountAll = ENABLE_UNMOUNT_ALL
? () => {
const [show, setShow] = React.useState(true)
// clear debug globals
setTimeout(() => {
debugClear()
}, 1000)
const unmountAll = <UnmountAll setShow={setShow} />
return {show, unmountAll}
}
: () => {
return {show: true, unmountAll: null}
}
export const useDebugLayout = __DEV__
? (cb?: () => void) => {
const sizeRef = React.useRef([0 as number, 0 as number] as const)
return React.useCallback(
(e: LayoutChangeEvent) => {
const height = e.nativeEvent.layout.height
const width = e.nativeEvent.layout.width
const [w, h] = sizeRef.current
sizeRef.current = [width, height]
if ((w && w !== width) || (h && h !== height)) {
console.log('[DEBUG] useDebugLayout', {
data: cb?.(),
h,
height,
w,
width,
})
}
},
[cb]
)
}
: () => {
return undefined
}