@render-props/size-observer
v0.1.18
Published
A state container which provides an interface for constantly querying for changes to the size of the referenced element in its child function.
Maintainers
Readme
SizeObserver
A state container which provides an interface for constantly querying for changes to the size of the referenced element in its child function.
Installation
yarn add @render-props/size-observer or npm i @render-props/size-observer
Usage
import SizeObserver from '@render-props/size-observer'
function SizeObservedDiv (props) {
return (
<SizeObserver every={100/**queries every hundred milliseconds*/}>
({sizeRef, width, height, recalcSize}) => (
<div ref={sizeRef}>
<div>
width: {width}
</div>
<div>
height: {height}
</div>
</div>
)
</SizeObserver>
)
}Props
every {number} {default: 1000/60}- the size observer will re-evaluate the size every
@everymilliseconds
- the size observer will re-evaluate the size every
useBoundingRect {bool} {default: false}- if
truethe element will be measured withgetBoundingClientRectinstead ofoffsetWidthandoffsetHeight
- if
onChange {function<{width, height}>}- called each time the width or height changes
Render Props
Ref
sizeRef- This
refmust be provided to whatever element you are trying to observe the the size of. e.g.<div ref={sizeRef}>
- This
Methods
recalcSize()- forces the component to re-calculate the size of the element
State
width {number}- the
offsetWidthorgetBoundingClientRect().widthof the element
- the
height {number}- the
offsetHeightorgetBoundingClientRect().heightof the element
- the
