Skip to content

Commit d5231a7

Browse files
committed
Pass size props on initial render - my solution to renatorib#7. Also add .editorconfig and use react-display-name package instead of local implementation
1 parent b3ee84b commit d5231a7

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 2
9+
indent_style = space
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"lodash.keys": "^4.2.0",
3838
"lodash.throttle": "^4.1.1",
3939
"react": "^15.5.3",
40+
"react-display-name": "^0.2.0",
4041
"uuid": "^3.0.1"
4142
},
4243
"devDependencies": {

src/Sizes.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ import { v4 } from 'uuid';
33
import keys from 'lodash.keys';
44
import throttle from 'lodash.throttle';
55

6-
import { getDisplayName } from './utils';
6+
import getDisplayName from 'react-display-name';
77

88
let resizeListener;
99
const listeners = {};
1010

1111
const Sizes = (...mappedSizesToProps) => (WrappedComponent) => {
12+
const parseMappedSizesToProps = ({ width, height }) => {
13+
const propsToPass = mappedSizesToProps
14+
.map(check => check({width, height}))
15+
.reduce((acc, props) => ({...acc, ...props}), {});
16+
return propsToPass
17+
}
18+
1219
return class extends Component {
1320
static displayName = `Sizes(${getDisplayName(WrappedComponent)})`;
1421

@@ -17,12 +24,20 @@ const Sizes = (...mappedSizesToProps) => (WrappedComponent) => {
1724
propsToPass: {},
1825
};
1926

27+
constructor(props) {
28+
super(props)
29+
this.state.propsToPass = parseMappedSizesToProps({
30+
width: window && window.innerWidth,
31+
height: window && window.innerHeight,
32+
})
33+
}
34+
2035
componentDidMount() {
2136
if (!resizeListener) {
2237
resizeListener = window.addEventListener('resize', this.throttledWindowResize);
2338
}
2439

25-
listeners[this.state.id] = this.parseMappedSizesToProps;
40+
listeners[this.state.id] = dimensions => this.setState({ propsToPass: parseMappedSizesToProps(dimensions) });
2641
this.dispatchSizes();
2742
}
2843

@@ -51,14 +66,6 @@ const Sizes = (...mappedSizesToProps) => (WrappedComponent) => {
5166
throttle(this.dispatchSizes, 200)
5267
);
5368

54-
parseMappedSizesToProps = ({ width, height }) => {
55-
const propsToPass = mappedSizesToProps
56-
.map(check => check({ width, height }))
57-
.reduce((acc, props) => ({ ...acc, ...props }), {});
58-
59-
this.setState({ propsToPass });
60-
}
61-
6269
render() {
6370
return <WrappedComponent {...this.props} {...this.state.propsToPass} />;
6471
}

src/utils/get-display-name.js

-8
This file was deleted.

src/utils/index.js

-1
This file was deleted.

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -3863,6 +3863,10 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.1.7:
38633863
minimist "^1.2.0"
38643864
strip-json-comments "~2.0.1"
38653865

3866+
react-display-name@^0.2.0:
3867+
version "0.2.0"
3868+
resolved "https://registry.yarnpkg.com/react-display-name/-/react-display-name-0.2.0.tgz#0e1f7086e45a32d07764df35ed32ff16f1259790"
3869+
38663870
react-docgen@^2.12.1:
38673871
version "2.13.0"
38683872
resolved "https://registry.yarnpkg.com/react-docgen/-/react-docgen-2.13.0.tgz#7fcc4a3104ea8d4fd428383ba38df11166837be9"

0 commit comments

Comments
 (0)