Search code examples
javascriptresize-observer

How to detect window resize event using ResizeObserver


I'd like to detect widnow resize event using ResizeObserver

currenlty I use

window.addEventListener('resize',() => console.log('resize'));

I want to refactor it and use ResizeObserver. Is it possible?


Solution

  • You can observe document.body.

    new ResizeObserver(() => {
      console.log('resized')
    }).observe(document.body)