Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Living wiki of UI design patterns and best practices built with Fumadocs, Next.js, and Base UI components.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/container-use-resize-observer.md
1---2title: Use ResizeObserver for Measurement3impact: MEDIUM4tags: container, resize-observer, performance5---67## Use ResizeObserver for Measurement89Use ResizeObserver to track element dimensions. It fires on resize without causing layout thrashing.1011**Incorrect (measuring on every render):**1213```tsx14function useMeasure(ref) {15const [bounds, setBounds] = useState({ width: 0, height: 0 });16useEffect(() => {17if (ref.current) {18const rect = ref.current.getBoundingClientRect();19setBounds({ width: rect.width, height: rect.height });20}21});22return bounds;23}24```2526**Correct (ResizeObserver):**2728```tsx29function useMeasure() {30const [element, setElement] = useState(null);31const [bounds, setBounds] = useState({ width: 0, height: 0 });32const ref = useCallback((node) => setElement(node), []);3334useEffect(() => {35if (!element) return;36const observer = new ResizeObserver(([entry]) => {37setBounds({38width: entry.contentRect.width,39height: entry.contentRect.height,40});41});42observer.observe(element);43return () => observer.disconnect();44}, [element]);4546return [ref, bounds];47}48```49