diff --git a/src/components/SizeView.tsx b/src/components/SizeView.tsx index 8d95ca26..d8b45fde 100644 --- a/src/components/SizeView.tsx +++ b/src/components/SizeView.tsx @@ -1,16 +1,18 @@ -import { memo, useCallback, useRef } from 'react' -import { type LayoutChangeEvent, StyleSheet, View, StatusBar } from 'react-native' +import { memo, useCallback, useRef, useEffect } from 'react' +import { type LayoutChangeEvent, StyleSheet, View, StatusBar, Dimensions } from 'react-native' import commonState from '@/store/common/state' import { setStatusbarHeight } from '@/core/common' import { windowSizeTools, getWindowSize } from '@/utils/windowSizeTools' export default memo(() => { - // const viewRef = useRef(null) const currentHeightRef = useRef(commonState.statusbarHeight) + const dimensionsChangedRef = useRef(true) const handleLayout = useCallback(({ nativeEvent: { layout } }: LayoutChangeEvent | { nativeEvent: { layout: { width: number, height: number } } }) => { // console.log('handleLayout') + if (!dimensionsChangedRef.current) return void getWindowSize().then(size => { + dimensionsChangedRef.current = false // console.log(layout, size) const height = parseFloat(size.height.toFixed(2)) >= parseFloat(layout.height.toFixed(2)) ? 0 @@ -27,18 +29,23 @@ export default memo(() => { } }) }, []) - // useEffect(() => { - // let timeout: NodeJS.Timeout | null = null - // Dimensions.addEventListener('change', () => { - // if (timeout) clearTimeout(timeout) - // timeout = setTimeout(() => { - // timeout = null - // viewRef.current?.measureInWindow((x, y, width, height) => { - // handleLayout({ nativeEvent: { layout: { width, height } } }) - // }) - // }, 100) - // }) - // }, []) + useEffect(() => { + // let timeout: NodeJS.Timeout | null = null + const subscription = Dimensions.addEventListener('change', () => { + dimensionsChangedRef.current = true + // if (timeout) clearTimeout(timeout) + // timeout = setTimeout(() => { + // timeout = null + // viewRef.current?.measureInWindow((x, y, width, height) => { + // handleLayout({ nativeEvent: { layout: { width, height } } }) + // }) + // }, 100) + }) + + return () => { + subscription.remove() + } + }, []) return () }, () => true)