2017-03-03 20:09:08 +00:00
|
|
|
import {PureComponent, PropTypes} from 'react';
|
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-01 00:10:23 +00:00
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
@autobind
|
2017-03-01 00:10:23 +00:00
|
|
|
class Lazy extends PureComponent {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2017-03-03 20:09:08 +00:00
|
|
|
this.state = {show: false};
|
|
|
|
}
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
show () {
|
|
|
|
this.setState({show: true});
|
2017-03-01 00:10:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
componentWillMount () {
|
|
|
|
if (this.props.delay < 0) {
|
|
|
|
// Show right away if negative delay passed
|
|
|
|
this.show();
|
|
|
|
} else {
|
|
|
|
setTimeout(this.show, this.props.delay || 50);
|
2017-03-01 21:15:56 +00:00
|
|
|
}
|
2017-03-01 00:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return this.state.show ? this.props.children : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lazy.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
delay: PropTypes.number,
|
|
|
|
children: PropTypes.node
|
2017-03-01 00:10:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Lazy;
|