Skip to content

Commit fb9ead1

Browse files
committed
Version 9.12.0 - Added defaultHeight and defaultWidth properties to AutoSizer for SSR
1 parent 0dc23b0 commit fb9ead1

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Changelog
22
------------
33

4+
##### 9.12.0
5+
* 🎉 Added `defaultWidth` and `defaultHeight` props to `AutoSizer` to better support server-side rendering.
6+
47
##### 9.11.1
58
* 🐛 `Masonry` component now properly pre-renders as specified by `overscanByPixels`
69

docs/AutoSizer.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ High-order component that automatically adjusts the width and height of a single
77
| Property | Type | Required? | Description |
88
|:---|:---|:---:|:---|
99
| children | Function || Function responsible for rendering children. This function should implement the following signature: `({ height: number, width: number }) => PropTypes.element` |
10+
| defaultHeight | Number | | Height passed to child for initial render; useful for server-side rendering. This value will be overridden with an accurate height after mounting. |
11+
| defaultWidth | Number | | Width passed to child for initial render; useful for server-side rendering. This value will be overridden with an accurate width after mounting. |
1012
| disableHeight | Boolean | | Fixed `height`; if specified, the child's `height` property will not be managed |
1113
| disableWidth | Boolean | | Fixed `width`; if specified, the child's `width` property will not be managed |
1214
| nonce | String | | Nonce of the inlined stylesheets for [Content Security Policy](https://www.w3.org/TR/2016/REC-CSP2-20161215/#script-src-the-nonce-attribute) |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "React components for efficiently rendering large, scrollable lists and tabular data",
44
"author": "Brian Vaughn <[email protected]>",
55
"user": "bvaughn",
6-
"version": "9.11.1",
6+
"version": "9.12.0",
77
"homepage": "https://github.com/bvaughn/react-virtualized",
88
"main": "dist/commonjs/index.js",
99
"module": "dist/es/index.js",

source/AutoSizer/AutoSizer.jest.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import React from 'react';
44
import {findDOMNode} from 'react-dom';
5+
import ReactDOMServer from 'react-dom/server';
56
import {render} from '../TestUtils';
67
import AutoSizer from './AutoSizer';
78

@@ -16,6 +17,8 @@ describe('AutoSizer', () => {
1617
{
1718
bar = 123,
1819
ChildComponent = DefaultChildComponent,
20+
defaultHeight = undefined,
21+
defaultWidth = undefined,
1922
disableHeight = false,
2023
disableWidth = false,
2124
foo = 456,
@@ -43,6 +46,8 @@ describe('AutoSizer', () => {
4346
return (
4447
<div style={style}>
4548
<AutoSizer
49+
defaultHeight={defaultHeight}
50+
defaultWidth={defaultWidth}
4651
disableHeight={disableHeight}
4752
disableWidth={disableWidth}
4853
onResize={onResize}>
@@ -218,4 +223,19 @@ describe('AutoSizer', () => {
218223
done();
219224
});
220225
});
226+
227+
describe('server-side rendering', () => {
228+
it('should render content with default widths and heights initially', () => {
229+
const rendered = ReactDOMServer.renderToString(
230+
getMarkup({
231+
defaultHeight: 100,
232+
defaultWidth: 200,
233+
height: 400,
234+
width: 800,
235+
}),
236+
);
237+
expect(rendered).toContain('height:100');
238+
expect(rendered).toContain('width:200');
239+
});
240+
});
221241
});

source/AutoSizer/AutoSizer.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ type Props = {
1515
/** Function responsible for rendering children.*/
1616
children: (warams: Size) => React.Element<*>,
1717

18+
/** Default height to use for initial render; useful for SSR */
19+
defaultHeight?: number,
20+
21+
/** Default width to use for initial render; useful for SSR */
22+
defaultWidth?: number,
23+
1824
/** Disable dynamic :height property */
1925
disableHeight: boolean,
2026

@@ -45,8 +51,8 @@ export default class AutoSizer extends React.PureComponent {
4551
props: Props;
4652

4753
state = {
48-
height: 0,
49-
width: 0,
54+
height: this.props.defaultHeight || 0,
55+
width: this.props.defaultWidth || 0,
5056
};
5157

5258
_parentNode: ?HTMLElement;
@@ -89,20 +95,24 @@ export default class AutoSizer extends React.PureComponent {
8995
// Outer div should not force width/height since that may prevent containers from shrinking.
9096
// Inner component should overflow and use calculated width/height.
9197
// See issue #68 for more information.
92-
const outerStyle: Object = {overflow: 'visible'};
98+
const outerStyle: any = {overflow: 'visible'};
99+
const childParams: any = {};
93100

94101
if (!disableHeight) {
95102
outerStyle.height = 0;
103+
childParams.height = height;
96104
}
97105

98106
if (!disableWidth) {
99107
outerStyle.width = 0;
108+
childParams.width = width;
100109
}
101110

102111
/**
103112
* TODO: Avoid rendering children before the initial measurements have been collected.
104113
* At best this would just be wasting cycles.
105114
* Add this check into version 10 though as it could break too many ref callbacks in version 9.
115+
* Note that if default width/height props were provided this would still work with SSR.
106116
if (
107117
height !== 0 &&
108118
width !== 0
@@ -113,7 +123,7 @@ export default class AutoSizer extends React.PureComponent {
113123

114124
return (
115125
<div ref={this._setRef} style={outerStyle}>
116-
{children({height, width})}
126+
{children(childParams)}
117127
</div>
118128
);
119129
}

0 commit comments

Comments
 (0)