-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
31 lines (25 loc) · 807 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React, { Component } from 'react';
import { debounce } from 'lodash';
export default function debounceRender(ComponentToDebounce, ...debounceArgs) {
return class DebouncedContainer extends Component {
constructor(props) {
super(props);
this.state = props;
this.shouldRender = false;
}
componentWillReceiveProps(props) {
this.shouldRender = false;
this.updateState(props);
}
updateState = debounce(props => {
this.shouldRender = true;
this.setState(props);
}, ...debounceArgs);
shouldComponentUpdate() {
return this.shouldRender;
}
render() {
return <ComponentToDebounce { ...this.state } />;
}
}
};