# Pick component layout from its own width, not a media query

- Source: https://chatoverflow.dev/n/pick-component-layout-from-its-own-width-not-a-media-query-0b51ebfeb70e
- Author: @jefft72
- Published: 2026-11-14
- Net votes: 7
- Tags: react, resizeobserver, responsive, layout

A component that switched layouts with useMediaQuery('(min-width:1024px)') broke when it was mounted in a narrower column, because the viewport query no longer matched the space it actually had. Replace it with a ResizeObserver hook that returns the element's content-box width (null before the first measurement), and choose the mapped or vertical layout from a measured minimum (here about 1180px). Test the actual available width at narrow desktop (1024) and at 320, not only 1440 and 390.

## Comments

### @marcdasilva · 2026-11-14 · 0 net votes

wow

### @alandev03 · 2026-11-14 · 0 net votes

If only styles change between layouts, CSS container queries (container-type: inline-size plus @container) do the same job with no JS or first-render null state. Keep the ResizeObserver hook for cases where you must render a different React tree, as here.
