You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Hooks](https://reactjs.org/docs/hooks-intro.html) are probably the easiest way to update your components but as of now needs to be consumed with care because this API and hooks in general are in early stage and thus are may contain bugs or will undergo breaking changes. As always, please report bugs if you find some.
90
-
91
89
**!!! Hooks require a `ViewportProvider` as a parent and only work with react v16.7.0 !!!**
92
90
93
91
### API
94
92
95
-
|Property| Type | Required? | Description |
93
+
|Argument| Type | Required? | Description |
96
94
|:---|:---|:---:|:---|
97
-
| disableScrollUpdates | boolean || Disables updates to scroll events (only for `useViewport` and `useLayoutSnapshot`) |
98
-
| disableDimensionsUpdates | boolean || Disables updates to dimensions events (only for `useViewport` and `useLayoutSnapshot`) |
99
-
| deferUpdateUntilIdle | boolean || Defers to trigger updates until the collector is idle. See [Defer Events](../concepts/defer_events.md)|
100
-
| priority |`'low'`, `'normal'`, `'high'`, `'highest'`|| Allows to set a priority of the update. See [Defer Events](../concepts/scheduler.md)|
95
+
|options.disableScrollUpdates | boolean || Disables updates to scroll events (only for `useViewport` and `useLayoutSnapshot`) |
96
+
|options.disableDimensionsUpdates | boolean || Disables updates to dimensions events (only for `useViewport` and `useLayoutSnapshot`) |
97
+
|options.deferUpdateUntilIdle | boolean || Defers to trigger updates until the collector is idle. See [Defer Events](../concepts/defer_events.md)|
98
+
|options.priority |`'low'`, `'normal'`, `'high'`, `'highest'`|| Allows to set a priority of the update. See [Defer Events](../concepts/scheduler.md)|
Hook effects allow to trigger side effects on change without updating the component.
124
+
125
+
**!!! Hooks require a `ViewportProvider` as a parent and only work with react v16.7.0 !!!**
126
+
127
+
### API
128
+
129
+
| Argument | Type | Required? | Description |
130
+
|:---|:---|:---:|:---|
131
+
| effect | (IViewport \| IScroll \| IDimensions) => void | x | Disables updates to scroll events (only for `useViewport`) |
132
+
| options.disableScrollUpdates | boolean || Disables updates to scroll events (only for `useViewport`) |
133
+
| options.disableDimensionsUpdates | boolean || Disables updates to dimensions events (only for `useViewport`) |
134
+
| options.deferUpdateUntilIdle | boolean || Defers to trigger updates until the collector is idle. See [Defer Events](../concepts/defer_events.md)|
135
+
| options.priority |`'low'`, `'normal'`, `'high'`, `'highest'`|| Allows to set a priority of the update. See [Defer Events](../concepts/scheduler.md)|
136
+
| options.recalculateLayoutBeforeUpdate | function || Enables a way to calculate layout information for all components as a badge before the effect call. Contains `IViewport`, `IScroll` or `IDimensions` as the first argument, dependent of the used hook. See [recalculateLayoutBeforeUpdate](../concepts/recalculateLayoutBeforeUpdate.md)|
Returns the rect of the elements, including it's position within the viewport as well as it's size.
6
+
7
+
Please note that at the moment the rect will only update after global scroll or resize events. Changes to the element without those interactions will not be observed (e.g. if an animation is performed).
8
+
In case you need full control over the element, I recommend using the [ResizeObserver DOM API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver).
9
+
10
+
**!!! Hooks require a `ViewportProvider` as a parent and only work with react v16.7.0 !!!**
11
+
12
+
### API
13
+
14
+
| Argument | Type | Required? | Description |
15
+
|:---|:---|:---:|:---|
16
+
| ref | React.RefObject\<HTMLElement> | x | The reference to an element that should be observed |
17
+
| options.disableScrollUpdates | boolean || Disables updates to scroll events (only for `useViewport`) |
18
+
| options.disableDimensionsUpdates | boolean || Disables updates to dimensions events (only for `useViewport`) |
19
+
| options.deferUpdateUntilIdle | boolean || Defers to trigger updates until the collector is idle. See [Defer Events](../concepts/defer_events.md)|
20
+
| options.priority |`'low'`, `'normal'`, `'high'`, `'highest'`|| Allows to set a priority of the update. See [Defer Events](../concepts/scheduler.md)|
21
+
22
+
### Example
23
+
24
+
```javascript
25
+
import*asReactfrom'react';
26
+
import { useRect } from'react-viewport-utils';
27
+
28
+
functionComponent() {
29
+
constref=React.useRef()
30
+
constrect=useRect(ref) || {
31
+
width:NaN,
32
+
height:NaN,
33
+
};
34
+
35
+
return (
36
+
<div ref={ref}>
37
+
Current Size is {rect.width}x{rect.width}
38
+
</div>
39
+
);
40
+
}
41
+
```
42
+
43
+
## `useRectEffect`
44
+
45
+
Same as the `useRect` hook but as an effect, therefore it does not return anything and will not re-render the component. This should be used if a side effect should be performed.
46
+
47
+
**!!! Hooks require a `ViewportProvider` as a parent and only work with react v16.7.0 !!!**
48
+
49
+
### API
50
+
51
+
| Argument | Type | Required? | Description |
52
+
|:---|:---|:---:|:---|
53
+
| effect | (rect: IRect \| null) => void | x | The side effect that should be performed |
54
+
| ref | React.RefObject\<HTMLElement> | x | The reference to an element that should be observed |
55
+
| options.disableScrollUpdates | boolean || Disables updates to scroll events (only for `useViewport`) |
56
+
| options.disableDimensionsUpdates | boolean || Disables updates to dimensions events (only for `useViewport`) |
57
+
| options.deferUpdateUntilIdle | boolean || Defers to trigger updates until the collector is idle. See [Defer Events](../concepts/defer_events.md)|
58
+
| options.priority |`'low'`, `'normal'`, `'high'`, `'highest'`|| Allows to set a priority of the update. See [Defer Events](../concepts/scheduler.md)|
0 commit comments