File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,20 @@ import airbnbBreakpoints from '../util/airbnb-breakpoints';
44
55const Context = React . createContext ( ) ;
66
7+ // debouncing function for kristof0425/react-with-breakpoints/29
8+ const debounce = ( func , interval ) => {
9+ let timeout ;
10+ return ( ...args ) => {
11+ const context = this ;
12+ let later = ( ) => {
13+ timeout = null ;
14+ func . apply ( context , args ) ;
15+ }
16+ clearTimeout ( timeout ) ;
17+ timeout = setTimeout ( later , interval ) ;
18+ } ;
19+ } ;
20+
721export const withBreakpoints = ( WrappedComponent ) => {
822 const Component = ( props ) => (
923 < Context . Consumer >
@@ -45,7 +59,8 @@ export default class BreakpointsProvider extends PureComponent {
4559 }
4660
4761 componentDidMount ( ) {
48- window . addEventListener ( 'resize' , this . handleResize , { passive : true } ) ;
62+ const debouncedResize = debounce ( this . handleResize , 50 ) ;
63+ window . addEventListener ( 'resize' , debouncedResize , { passive : true } ) ;
4964 this . handleResize ( ) ;
5065 }
5166
You can’t perform that action at this time.
0 commit comments