Transparently retrieve a component size
npm install react-size-fetcherReactSizeFetcher is a simple-to-use React library to transparently and dynamically retrieve the DOM sizes of a sub-component.
It is a tiny (8kB) library.
  
```
npm install --save react-size-fetcher
SizeFetcher is a Higher Order Component; by giving it a component, it will return an enhanced component.
`javascript`
const EnhancedComponent = SizeFetcher(ComponentToObserve, [options])sizeChange
The enhanced component is special, it will be a copy of the given component but will accept a new prop called .`javascript`Object
sizeChange is a function with one argument, it will be called with an representing the size of the component when the component did mount and when its size change.`javascript`
// Size Changed: { clientWidth: 120, clientHeight: 230, scrollWidth: 120, scrollHeight: 430 }
if the size did not change between two updates.
const EnhancedComponent = SizeFetcher(ComponentToObserve, { noComparison: true})
* [watchSubComponents] (Array): Default value: []. This option allow you to indicate if you want to watch specific sub-components. When defined, SizeFetcher will go through the render method of each sub component of the ComponentToObserve and add a proxy that will call sizeChange function when they update.
const EnhancedComponent = SizeFetcher(ComponentToObserve, { watchSubComponents: ['AccordionComponent', 'ListComponent'] })
$3
A Higher-Order React Component that inherit from your initial component and take one more props named sizeChange. sizeChange is suceptible to be called when the component receives new props, updates its state or when the window resize.$3
Here is a simple way to use the library:`javascript
import SizeFetcher from 'react-size-fetcher'
import ComponentToObserve from './ComponentToObserve'
const EnhancedComponent = SizeFetcher(ComponentToObserve)class AwareComponent extends React.Component {
constructor() {
super()
this.state = {
subComponentSize: null
}
}
render() {
const { subComponentSize } = this.state
return (
The size of the sub component is {JSON.stringify(subComponentSize, null, 2)}
this.setState({ subComponentSize: size })} {/ ComponentToObserve usual props /} />
)
}
}
`You can also enhance directly your ComponentToObserve by exporting if with SizeFetcher in your ComponentToObserve.js file:
`
export default SizeFetcher(ComponentToObserve)
`
or with a decorator
`
@SizeFetcher
class ComponentToObserve extends React.Component {
...
`Some advanced examples can be find in the docs folder of this repository. Live example also works.
How does it work ?
SizeFetcher is what we call an inverse inheritance higher order function. It takes a component as parameters and return a component; which by inheriting from the ComponentToObserve, will be able to highjack the render of ComponentToObserve and add functions to its lifecycle.
$3
The render highjack is usefull for SizeFetcher in two ways:
1. To add a ref) to the componentToObserve output to retrieve and store its DOM element.
2. To enhance the rendered children if the watchSubComponents list option is not empty.$3
SizeFetcher will modify three basic functions of ComponentToObserve's lifecycle:
1. componentDidMount: Add an event listener on the window resize.
2. componentWillUnmount: Remove the event listener on the window resize.
3. componentDidUpdate: Call the SizeFetcher returned component's private handleSizeMayHaveChanged function.The window resize listener will also call
handleSizeMayHaveChanged function on resize.$3
This function calls the given ComponentToObserve's sizeChange prop if the size of the DOM element (retrieved earlier at highjacking step 1.) did change compared to the last call values. The option noComparison will bypass the comparison and call sizeChange at every handleSizeMayHaveChange call.$3
This is were the magic happens. SizeFetcher will go through every sub component and look up for components' name that are in the watchSubComponents list and add to them a props called sizeMayChange. This props will be called everytime the sub-component updates. When this props is called, it will call the SizeFetcher returned component's private handleSizeMayHaveChanged` function and trigger the sizeChange if pertinent.You can find every release documented on the Releases page.