@@ -3,12 +3,19 @@ import { v4 } from 'uuid';
3
3
import keys from 'lodash.keys' ;
4
4
import throttle from 'lodash.throttle' ;
5
5
6
- import { getDisplayName } from './utils ' ;
6
+ import getDisplayName from 'react-display-name ' ;
7
7
8
8
let resizeListener ;
9
9
const listeners = { } ;
10
10
11
11
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
+
12
19
return class extends Component {
13
20
static displayName = `Sizes(${ getDisplayName ( WrappedComponent ) } )` ;
14
21
@@ -17,12 +24,20 @@ const Sizes = (...mappedSizesToProps) => (WrappedComponent) => {
17
24
propsToPass : { } ,
18
25
} ;
19
26
27
+ constructor ( props ) {
28
+ super ( props )
29
+ this . state . propsToPass = parseMappedSizesToProps ( {
30
+ width : window && window . innerWidth ,
31
+ height : window && window . innerHeight ,
32
+ } )
33
+ }
34
+
20
35
componentDidMount ( ) {
21
36
if ( ! resizeListener ) {
22
37
resizeListener = window . addEventListener ( 'resize' , this . throttledWindowResize ) ;
23
38
}
24
39
25
- listeners [ this . state . id ] = this . parseMappedSizesToProps ;
40
+ listeners [ this . state . id ] = dimensions => this . setState ( { propsToPass : parseMappedSizesToProps ( dimensions ) } ) ;
26
41
this . dispatchSizes ( ) ;
27
42
}
28
43
@@ -51,14 +66,6 @@ const Sizes = (...mappedSizesToProps) => (WrappedComponent) => {
51
66
throttle ( this . dispatchSizes , 200 )
52
67
) ;
53
68
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
-
62
69
render ( ) {
63
70
return < WrappedComponent { ...this . props } { ...this . state . propsToPass } /> ;
64
71
}
0 commit comments