Nowadays browsers start supporting element resize handling natively using ResizeObserver. And we use this feature (with polyfill) to help you handle element resizes in React.
For older implementations please checkout this branch v.1.1.0
Local demo:
git clone https://github.com/maslianok/react-resize-detector.git
cd react-resize-detector/examples
npm i && npm start
npm i react-resize-detector
// OR
yarn add react-resize-detector
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';
class App extends PureComponent {
render() {
return (
<div>
...
<ReactResizeDetector handleWidth handleHeight onResize={this.onResize} />
</div>
);
}
onResize = () => {
...
}
}
render(<App />, document.getElementById('root'));
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';
class App extends PureComponent {
render() {
return (
<div>
...
<ReactResizeDetector handleWidth handleHeight>
{(width, height) => (<div>{width}x{height}</div>)}
</ReactResizeDetector>
</div>
);
}
}
render(<App />, document.getElementById('root'));
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';
const ChildrenComponent = ({ width, height }) => (<div>{width}x{height}</div>);
class App extends PureComponent {
render() {
return (
<div>
...
<ReactResizeDetector handleWidth handleHeight>
<ChildrenComponent />
</ReactResizeDetector>
</div>
);
}
}
render(<App />, document.getElementById('root'));
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';
const ChildrenComponent = ({ width, height }) => (<div>{width}x{height}</div>);
class App extends PureComponent {
render() {
return (
<div>
...
<ReactResizeDetector handleWidth handleHeight onResize={this.onResize}>
{(width, height) => (<div>{width}x{height}</div>)}
<ChildrenComponent />
</ReactResizeDetector>
</div>
);
}
onResize = () => {
...
}
}
render(<App />, document.getElementById('root'));
Note: You can use multiple render methods for one call. When using render children, in mount children component will not received width and height value because window not resized, you should add a default value.
(Func) Function that will be invoked with width
and height
arguments.
(Bool) Trigger onResize
on width change. Default: false
.
(Bool) Trigger onResize
on height change. Default: false
.
(Bool) Do not trigger onResize when a component mounts. Default: false
.
(String) Id of the element we want to observe. If no one provided, parentElement of the component will be used. Default: ``.
(String) Possible values: throttle
and debounce
See lodash docs for more information. Default: undefined
- means that callback will be fired as often as ResizeObserver allows
(Number) Makes sense only when refreshMode
is set. Default: 1000
. Important! It's numeric prop so set it correctly, e.g. refreshRate={500}
MIT