@@ -24,7 +24,7 @@ yarn add use-resize-observer --dev
24
24
npm install use-resize-observer --save-dev
25
25
```
26
26
27
- ## Usage
27
+ ## Basic Usage
28
28
29
29
``` js
30
30
import React from " react" ;
@@ -43,7 +43,28 @@ const App = () => {
43
43
44
44
## Passing in your own ` ref `
45
45
46
- TODO
46
+ You can pass in your own ref to measure, use now.
47
+ This can be useful if you already have a ref from somewhere you want to measure.
48
+
49
+ ``` js
50
+ const { ref , width , height } = useResizeObserver ({
51
+ defaultWidth: 100 ,
52
+ defaultHeight: 50
53
+ });
54
+ ```
55
+
56
+ You can even reuse the same hook instance to measure different elements:
57
+
58
+ [ CodeSandbox Demo] ( https://codesandbox.io/s/use-resize-observer-reusing-refs-buftd )
59
+
60
+ ## Throttle / Debounce
61
+
62
+ You might want values less frequently than actually reported.
63
+
64
+ While this hook does not come its own implementation of throttling / debouncing
65
+ the reported values (Issue #19 ), you can use hook composition to achieve it:
66
+
67
+ [ CodeSandbox Demo] ( https://codesandbox.io/s/use-resize-observer-throttle-and-debounce-8uvsg )
47
68
48
69
## SSR, Default Size
49
70
@@ -54,17 +75,37 @@ const { ref, width, height } = useResizeObserver({
54
75
defaultWidth: 100 ,
55
76
defaultHeight: 50
56
77
});
78
+ ```
79
+
80
+ Here "width" and "height" will be 100 and 50 respectively, until the
81
+ ResizeObserver kicks in and reports the actual size.
82
+
83
+ ## Without Default Measurements
57
84
58
- // width / height will be 100 and 50 respectively, until the ResizeObserver
59
- // kicks in and reports the actual size.
85
+ If you only want real measurements (only values from the ResizeObserver without
86
+ defaults), then you can use the following:
87
+
88
+ ``` js
89
+ const { ref , width , height } = useResizeObserver ({
90
+ useDefaults: false
91
+ });
60
92
```
61
93
62
- ## Contributing
94
+ Here "width" and "height" will be undefined until the ResizeObserver takes its
95
+ first measurement.
96
+
97
+ ## Container/Element Query with CSS-in-JS
98
+
99
+ It's possible to apply styles conditionally based on the width / height of an
100
+ element using a CSS-in-JS solution, which is the basic idea behind
101
+ container/element queries:
102
+
103
+ [ CodeSandbox Demo] ( https://codesandbox.io/s/use-resize-observer-container-query-with-css-in-js-iitxl )
63
104
64
105
## Related
65
106
66
- - [ @zeecoder/react-resize-observer ] ( https://github.com/ZeeCoder/react-resize-observer )
67
107
- [ @zeecoder/container-query ] ( https://github.com/ZeeCoder/container-query )
108
+ - [ @zeecoder/react-resize-observer ] ( https://github.com/ZeeCoder/react-resize-observer )
68
109
69
110
## License
70
111
0 commit comments