Skip to content

Commit 7229ded

Browse files
committed
v0.2.0; add SSR to README + update build/dist js
addresses issue #1
1 parent bf2400c commit 7229ded

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ npm install -D use-viewport-sizes
99
```
1010

1111
## Benefits
12-
- extremely lightweight and dependency-free; **2.54kb pre-gzipped** with all dependencies.
12+
- extremely lightweight and dependency-free; **2.59kb pre-gzipped** with all dependencies.
1313
- only one `window.onresize` handler used to subscribe to any changes in an unlimited number of components.
1414
- 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.
1515
- debouncing does not create new handlers or waste re-renders in your component; the results are also pooled from only one resize result.
16+
- supports SSR.
1617

1718
## Usage ##
1819

@@ -24,6 +25,7 @@ npm install -D use-viewport-sizes
2425
*registers dimension changes on every resize event immediately*
2526

2627
```
28+
import React from 'react'
2729
import useViewportSizes from 'use-viewport-sizes'
2830
2931
function MyComponent (props) {
@@ -38,6 +40,7 @@ function MyComponent (props) {
3840
*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
3941
expensive to re-render during window resize dragging.*
4042
```
43+
import React from 'react'
4144
import useViewportSizes from 'use-viewport-sizes'
4245
4346
function MyExpensivelyRenderedComponent (props) {
@@ -47,3 +50,25 @@ function MyExpensivelyRenderedComponent (props) {
4750
}
4851
```
4952

53+
### **Server Side Rendering**
54+
55+
*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/).*
56+
57+
*Sidenote that you will see a `useLayoutEffect` warning from React. This is perfectly normal as there is no viewport/context to paint to when pre-rendering in SSR and will not interfere with your app once served to the client*
58+
59+
```
60+
import React, { useLayoutEffect } from 'react'
61+
import useViewportSizes from 'use-viewport-sizes'
62+
63+
function MySSRComponent (props) {
64+
const [vpWidth, vpHeight, updateVpSizes] = useViewportSizes()
65+
66+
// below, we add one post-render update
67+
// in order to register the client's viewport sizes
68+
// after serving SSR content
69+
70+
useEffect(()=> { updateVpSizes(); }, []);
71+
72+
...renderLogic
73+
}
74+
```

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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-viewport-sizes",
3-
"version": "0.1.10",
3+
"version": "0.2.0",
44
"description": "tiny React hook which allows you to track visible window viewport size in your components w/ an optional debounce for updates for optimal rendering.",
55
"main": "build/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)