@@ -23,31 +23,94 @@ npm install react-resize-aware --save
2323> ** note** : ` ResizeAware ` needs a position different from ` initial ` to work!
2424> Make sure to set it to ` relative ` , ` absolute ` or ` fixed ` using its ` style ` property or with CSS
2525
26+ ## Stateless approach
27+
28+ If your component is stateless or you prefer to follow a functional approach
29+ you can use ResizeAware to wrap your existing component and it will take care
30+ to provide two property (` height ` and ` width ` ) that will get updated every time
31+ the component sizes change.
32+
2633``` jsx
2734import React from ' react' ;
2835import ResizeAware from ' react-resize-aware' ;
2936
30- // This component will get rerendered everytime its width or height changes
37+ // This component will get re-rendered every time its width or height changes
3138function MyComponent ({width, height}) {
3239 return < div> {width}x{height}< / div> ;
3340}
3441
3542function App () {
3643 return (
37- < ResizeAware onResize = {({width, height}) => console . log (width, height) }>
44+ < ResizeAware style = {{ position : ' relative ' } }>
3845 < MyComponent / >
3946 < / ResizeAware>
4047 );
4148}
49+ ```
50+
51+ ## Stateful approach
4252
53+ If your component is stateful or you need to use ResizeAware in the middle of one
54+ of your components you can use the ` onResize ` property of the component to provide
55+ a callback that will be called on each resize of the ResizeAware component and will
56+ provide as first argument an object with ` width ` and ` height ` properties.
57+
58+ ``` jsx
59+ import React , { Component } from ' react' ;
60+ import ResizeAware from ' react-resize-aware' ;
61+
62+ function MyComponent ({width, height}) {
63+ return < div> {width}x{height}< / div> ;
64+ }
65+
66+ class MyComponent extend Component {
67+ handleResize = ({ width, height }) => console .log (width, height);
68+
69+ render () {
70+ return (
71+ < div>
72+ My app renders...
73+ < ResizeAware
74+ style= {{ position: ' relative' }}
75+ onlyEvent
76+ onResize= {this .handleResize }
77+ >
78+ < MyComponent / >
79+ < / ResizeAware>
80+ < / div>
81+ );
82+ }
83+ }
4384```
4485
45- The child component of ` ResizeAware ` will have two new properties (` width ` and ` height ` )
46- always up to date with the real size of the element.
86+ ## Properties
4787
48- Also, you can define an optional ` onResize ` property to make ` ResizeAware ` call
49- it on each resize and get the updated sizes.
88+ - The ` onlyEvent ` property will prevent ResizeAware from passing the ` height ` and ` width `
89+ properties to its child component, in case you don't want to rely on them.
90+ - The ` tag ` property allows to define the HTML tag used by ResizeAware to render.
91+ - The ` onResize ` property is an optional callback called on each resize with as first
92+ argument an object containing ` height ` and ` width ` properties.
5093
94+ <!-- ## Self containing
95+
96+ If you need to keep your DOM structure clean and you don't want the additional
97+ `div` added by ResizeAware, you can use the component as base for your own one.
98+
99+ ```jsx
100+ import React from 'react';
101+ import ResizeAware from 'react-resize-aware';
102+
103+ // This component will get re-rendered every time its width or height changes
104+ function MyComponent({width, height}) {
105+ return <div style={{ position: 'relative' }}>{width}x{height}</div>;
106+ }
107+
108+ function App() {
109+ return (
110+ <ResizeAware component={MyComponent} />
111+ );
112+ }
113+ ``` -->
51114
52115# License
53116
0 commit comments