Loading...
window.addEventListener('resize', () => {
chart.resize(container.offsetWidth);
});const ro = new ResizeObserver(([entry]) => {
chart.resize(entry.contentRect.width);
});
ro.observe(container);ResizeObserver catches size changes from layout, not just window resizes, and gives the new size without extra reads.
disconnect() or unobserve() when the component is removed.getBoundingClientRect() inside the callback.ResizeObserver.resize event is not used to track the size of a single element.The callback changed sizes that caused more resizes in the same frame. It is usually harmless, but avoid resizing the observed element in its own callback.
Use container queries when only styles change. Use ResizeObserver when JavaScript must react, like redrawing a canvas or chart.