maslianok / react-resize-detector

A Cross-Browser, Event-based, Element Resize Detection for React
http://maslianok.github.io/react-resize-detector/
MIT License
1.25k stars 91 forks source link

How do I get window width? #73

Closed Ivan5646 closed 5 years ago

Ivan5646 commented 5 years ago
                    <ReactResizeDetector
                        handleWidth
                        handleHeight
                        render={({ width, height }) => (
                            <div>
                                Width:{width}, Height:{height}
                            </div>
                        )}
                    />

With this example I get the width of the parent component

maslianok commented 5 years ago

Hi @Ivan5646,

You don't need this library to get window width or to handle window resizes.

There is a built-in JS method to work with window:

window.onresize = () => {
  console.log(window.innerWidth);
  console.log(window.innerHeight);
}

The main purpose of this library is to handle element resizes. But, if you want, you can always get window dimensions inside the callback

<ReactResizeDetector
                        handleWidth
                        handleHeight
                        render={({ width, height }) => (
                            <div>
                                Window width:{window.innerWidth}, height:{window.innerHeight}
                            </div>
                        )}
                    />

But pay attention that this callback will be executed only when the parent div gets resized.