@@ -3,12 +3,19 @@ import { v4 } from 'uuid';
33import keys from 'lodash.keys' ;
44import throttle from 'lodash.throttle' ;
55
6- import { getDisplayName } from './utils ' ;
6+ import getDisplayName from 'react-display-name ' ;
77
88let resizeListener ;
99const listeners = { } ;
1010
1111const 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 }
0 commit comments