Skip to content

Commit 39fe4cf

Browse files
authored
Merge pull request #5 from rob2d/feature/custom_hashing_functions
v0.2.8, add support for changing on custom-hash function output changes
2 parents 857f6c1 + 884262e commit 39fe4cf

File tree

5 files changed

+5350
-48
lines changed

5 files changed

+5350
-48
lines changed

README.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ npm install -D use-viewport-sizes
1111
```
1212

1313
## Benefits ##
14-
- extremely lightweight and dependency-free -- **2.25kb** before gzipping.
15-
- only one `window.onresize` handler used to subscribe to any changes in an unlimited number of components.
14+
- extremely lightweight and dependency-free -- **3.75kb** without/before gzipping.
15+
- only one `window.onresize` handler used to subscribe to any changes in an unlimited number of components as well despite several different paradigms that pull from this.
1616
- optional debounce to delay updates until user stops dragging their window for a moment; this can make expensive components with size-dependent calculations run much faster and your app feel smoother.
1717
- debouncing does not create new handlers or waste re-renders in your component; the results are also pooled from only one resize result.
18-
- supports SSR.
18+
- optional hash function to update component subtree only at points you would like to.
19+
- supports SSR (see example under Usage section).
20+
1921

2022
## Usage ##
2123

@@ -36,17 +38,18 @@ npm install -D use-viewport-sizes
3638
import React from 'react'
3739
import useViewportSizes from 'use-viewport-sizes'
3840

39-
function MyComponent (props) {
41+
function MyComponent(props) {
4042
const [vpWidth, vpHeight] = useViewportSizes();
4143

4244
...renderLogic
4345
}
4446
```
4547

4648

47-
### **With Debouncing**
48-
*registers dimension changes only when a user stops dragging/resizing the window for a specified number of miliseconds; for expensive components such as data grids which may be too
49-
expensive to re-render during window resize dragging.*
49+
### **With Debouncing**
50+
51+
If passed a number as the first argument, it registers dimension changes only when a user stops dragging/resizing the window for a specified number of miliseconds. This is useful for listening to expensive components such as data grids which may be too
52+
expensive to re-render during window resize dragging.
5053
```js
5154
import React from 'react'
5255
import useViewportSizes from 'use-viewport-sizes'
@@ -58,7 +61,38 @@ function MyExpensivelyRenderedComponent (props) {
5861
}
5962
```
6063

61-
### **Server Side Rendering**
64+
### **Only update vpW/vpH passed on specific conditions**
65+
If passed a function as the first argument, it will use this to calculate a hash that only updates the viewport when the calculation changes. In the example here, we are using it to detect when we have a breakpoint change which may change how a component is rendered if this is not fully possible or inconvenient via CSS `@media` queries. The hash will also be available as the 3rd value returned from the hook for convenience.
66+
```js
67+
import React from 'react';
68+
import useViewportSizes from 'use-viewport-sizes';
69+
70+
function getBreakpoint({ vpW, vpH }) {
71+
if(vpW < 640) {
72+
return 'md';
73+
}
74+
if(vpW < 320) {
75+
return 'sm';
76+
}
77+
else if(vpW < 240) {
78+
return 'xs';
79+
}
80+
else {
81+
return 'lg';
82+
}
83+
}
84+
85+
function MyBreakpointBehaviorComponent() {
86+
const [vpW, vpH, bp] = useViewportSizes(getBreakpoint);
87+
88+
// do-something-with-breakpoints in render
89+
// and add new update for vpW, vpH in this component's
90+
// subtree only when a named breakpoint changes
91+
}
92+
```
93+
94+
95+
### **Server Side Rendering**
6296

6397
*While serverside rendering is supported, it requires an explicit update via `useEffect` since viewport does not actually exist on the server before rendering to client. The following has been tested with [NextJS](https://nextjs.org/).*
6498

@@ -82,11 +116,10 @@ function MySSRComponent (props) {
82116
```
83117

84118
## Support
85-
If you have read the examples and have any issues which you know are glitches,or would like to request something changed, please feel free to [post an issue on Github](https://github.com/rob2d/use-viewport-sizes/issues/new).
119+
If you have read the examples and have any issues which you know are glitches,or would like to request something changed, please feel free to [post an issue on Github](https://github.com/rob2d/use-viewport-sizes/issues/new).
86120

87-
Otherwise, if this was useful and you'd like to show your support, no donations necessary, but please consider [checking out the repo](https://github.com/rob2d/use-viewport-sizes) and giving a star.
121+
Otherwise, if this was useful and you'd like to show your support, no donations necessary, but please consider [checking out the repo](https://github.com/rob2d/use-viewport-sizes) and giving it a star (⭐).
88122

89123
## License ##
90124

91-
- Open Source **[MIT license](http://opensource.org/licenses/mit-license.php)**
92-
- 2019 © <a href="http://robertconcepcion.com" target="_blank">Robert Concepción III</a>
125+
- Open Source **[MIT license](http://opensource.org/licenses/mit-license.php)**

build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)