import TestCase from '../../TestCase'; import Fixture from '../../Fixture'; const React = window.React; const {Fragment, useEffect, useRef, useState} = React; export default function ResizeObserverCase() { const fragmentRef = useRef(null); const [width, setWidth] = useState([0, 0, 0]); useEffect(() => { const resizeObserver = new window.ResizeObserver(entries => { if (entries.length > 0) { setWidth(prev => { const newWidth = [...prev]; entries.forEach(entry => { const index = parseInt(entry.target.id, 10); newWidth[index] = Math.round(entry.contentRect.width); }); return newWidth; }); } }); fragmentRef.current.observeUsing(resizeObserver); const lastFragmentRefValue = fragmentRef.current; return () => { lastFragmentRefValue.unobserveUsing(resizeObserver); }; }, []); return (
  • Resize the viewport width until the children respond
  • See that the width data updates as they elements resize
  • The Fragment Ref has a ResizeObserver attached which has a callback to update the width state of each child node.

    Width: {width[0]}px

    Width: {width[1]}px

    Width: {width[2]}px

    ); }