From b2abf09c99864861e69c3a1fe3bbf801aaa6f4a5 Mon Sep 17 00:00:00 2001 From: lerte smith Date: Thu, 7 Nov 2024 02:11:26 +0800 Subject: [PATCH] use ResizeObserver replace window resize --- packages/actify/src/components/Select/Select.tsx | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/actify/src/components/Select/Select.tsx b/packages/actify/src/components/Select/Select.tsx index 5014998..7c5bf4d 100644 --- a/packages/actify/src/components/Select/Select.tsx +++ b/packages/actify/src/components/Select/Select.tsx @@ -38,19 +38,14 @@ const Select = (props: SelectProps) => { Tag = OutlinedField } - React.useLayoutEffect(() => { - const width = ref?.current?.getBoundingClientRect().width - setReferenceWidth(width) - }, []) - React.useEffect(() => { - const updateWidth = () => { + const resizeObserver = new ResizeObserver(() => { const width = ref?.current?.getBoundingClientRect().width setReferenceWidth(width) - } - window.addEventListener('resize', updateWidth) + }) + resizeObserver.observe(ref?.current as Element) return () => { - window.removeEventListener('resize', updateWidth) + resizeObserver.disconnect() } }, [])