2018-06-25 17:42:50 +00:00
|
|
|
import { PureComponent } from 'react';
|
2017-08-10 01:56:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 20:09:08 +00:00
|
|
|
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 {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props) {
|
2017-03-01 00:10:23 +00:00
|
|
|
super(props);
|
2018-06-25 17:42:50 +00:00
|
|
|
this.state = { show: false };
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
show() {
|
|
|
|
this.setState({ show: true });
|
2017-03-01 00:10:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentWillMount() {
|
2017-03-03 20:09:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2017-03-01 00:10:23 +00:00
|
|
|
return this.state.show ? this.props.children : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Lazy.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
delay: PropTypes.number,
|
2018-12-12 17:36:11 +00:00
|
|
|
children: PropTypes.node,
|
2017-03-01 00:10:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Lazy;
|