@@ -37,16 +37,14 @@ const App = () => {
37
37
};
38
38
```
39
39
40
- ## Passing in your own ` ref `
40
+ ## Passing in Your Own ` ref `
41
41
42
- You can pass in your own ref to measure, use now .
43
- This can be useful if you already have a ref from somewhere you want to measure.
42
+ You can pass in your own ref instead of using the one provided .
43
+ This can be useful if you already have a ref you want to measure.
44
44
45
45
``` js
46
- const { ref , width , height } = useResizeObserver ({
47
- defaultWidth: 100 ,
48
- defaultHeight: 50
49
- });
46
+ const ref = useRef (null );
47
+ const { width , height } = useResizeObserver ({ ref });
50
48
```
51
49
52
50
You can even reuse the same hook instance to measure different elements:
@@ -55,16 +53,21 @@ You can even reuse the same hook instance to measure different elements:
55
53
56
54
## Throttle / Debounce
57
55
58
- You might want values less frequently than actually reported .
56
+ You might want to receive values less frequently than changes actually occur .
59
57
60
- While this hook does not come its own implementation of throttling / debouncing
61
- the reported values (Issue # 19 ), you can use hook composition to achieve it :
58
+ While this hook does not come with its own implementation of throttling / debouncing,
59
+ you can use hook composition instead to achieve the same results :
62
60
63
61
[ CodeSandbox Demo] ( https://codesandbox.io/s/use-resize-observer-throttle-and-debounce-8uvsg )
64
62
65
- ## SSR, Default Size
63
+ ## Defaults (SSR)
66
64
67
- You can set the default size, which is useful for SSR.
65
+ On initial mount the ResizeObserver will take a little time to report on the
66
+ actual size.
67
+
68
+ Until then the hook receives the first measurement, it returns with "1x1" by default.
69
+
70
+ You can override this behaviour, which could be useful for SSR as well.
68
71
69
72
``` js
70
73
const { ref , width , height } = useResizeObserver ({
@@ -76,10 +79,10 @@ const { ref, width, height } = useResizeObserver({
76
79
Here "width" and "height" will be 100 and 50 respectively, until the
77
80
ResizeObserver kicks in and reports the actual size.
78
81
79
- ## Without Default Measurements
82
+ ## No Defaults
80
83
81
84
If you only want real measurements (only values from the ResizeObserver without
82
- defaults ), then you can use the following:
85
+ any default values ), then you can use the following:
83
86
84
87
``` js
85
88
const { ref , width , height } = useResizeObserver ({
@@ -102,18 +105,16 @@ container/element queries:
102
105
103
106
By default the library provides transpiled ES5 modules in CJS / ESM module formats.
104
107
105
- Polyfilling is recommended to be done in the host app, and not in the library, as
106
- that gives users more control over what exact polyfills they might want to use .
108
+ Polyfilling is recommended to be done in the host app, and not within imported
109
+ libraries, as that way consumers have control over the exact polyfills being used .
107
110
108
- However , there's a polyfilled CJS module (without affecting globals) that can be
109
- used for convenience:
111
+ That said , there's a [ polyfilled] ( https://github.com/que-etc/resize-observer-polyfill )
112
+ CJS module that can be used for convenience (Not affecting globals) :
110
113
111
114
``` js
112
115
import useResizeObserver from " use-resize-observer/polyfilled" ;
113
116
```
114
117
115
- [ Bundled ResizeObserver implementation] ( < [ResizeObserver](https://github.com/que-etc/resize-observer-polyfill) > )
116
-
117
118
## Related
118
119
119
120
- [ @zeecoder/container-query ] ( https://github.com/ZeeCoder/container-query )
0 commit comments