DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Resize Observer: Responsive Components Without Media Queries
XLinkedInReddit
Frontend Engineering

Resize Observer: Responsive Components Without Media Queries

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • Basic Usage
  • React Hook
  • Use Cases
  • 1. Responsive Charts
  • 2. Text Truncation
  • 3. Dynamic Grid Columns
  • Performance Tips

ResizeObserver watches element size changes — not the viewport. This enables truly responsive components.

Basic Usage

const observer = new ResizeObserver((entries) => {
  for (const entry of entries) {
    const { width, height } = entry.contentRect;
    console.log(`Element resized: ${width}x${height}`);
    
    // Apply responsive classes based on element size
    if (width < 300) {
      entry.target.classList.add("compact");
    } else {
      entry.target.classList.remove("compact");
    }
  }
});

observer.observe(document.querySelector(".card"));

React Hook

function useResizeObserver(ref) {
  const [dimensions, setDimensions] = useState({ width: 0, height: 0 });

  useEffect(() => {
    if (!ref.current) return;

    const observer = new ResizeObserver((entries) => {
      const { width, height } = entries[0].contentRect;
      setDimensions({ width, height });
    });

    observer.observe(ref.current);
    return () => observer.disconnect();
  }, [ref]);

  return dimensions;
}

// Usage
function ResponsiveCard() {
  const ref = useRef(null);
  const { width } = useResizeObserver(ref);

  return (
    <div ref={ref} className={width < 400 ? "card-compact" : "card-full"}>
      {width < 400 ? <CompactView /> : <FullView />}
    </div>
  );
}

Use Cases

1. Responsive Charts

function ResponsiveChart({ data }) {
  const containerRef = useRef();
  const { width, height } = useResizeObserver(containerRef);

  return (
    <div ref={containerRef} style={{ width: "100%", height: "400px" }}>
      <Chart data={data} width={width} height={height} />
    </div>
  );
}

2. Text Truncation

function TruncatedText({ text }) {
  const ref = useRef();
  const { width } = useResizeObserver(ref);
  const maxChars = Math.floor(width / 8); // Rough estimate

  return (
    <div ref={ref}>
      {text.length > maxChars ? text.slice(0, maxChars) + "..." : text}
    </div>
  );
}

3. Dynamic Grid Columns

function AutoGrid({ children, minColumnWidth = 250 }) {
  const ref = useRef();
  const { width } = useResizeObserver(ref);
  const columns = Math.max(1, Math.floor(width / minColumnWidth));

  return (
    <div
      ref={ref}
      style={{
        display: "grid",
        gridTemplateColumns: `repeat(${columns}, 1fr)`,
        gap: "16px"
      }}
    >
      {children}
    </div>
  );
}

Performance Tips

  • ResizeObserver callbacks are debounced by the browser (after layout, before paint)
  • Avoid layout changes inside the callback that trigger more resizes (infinite loop risk)
  • Use a single observer instance for multiple elements when possible
  • Disconnect when component unmounts

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Basic Usage
  • React Hook
  • Use Cases
  • 1. Responsive Charts
  • 2. Text Truncation
  • 3. Dynamic Grid Columns
  • Performance Tips

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.