-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (32 loc) · 983 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
32
33
34
import React, { Component } from "react";
import { PropTypes } from "prop-types";
export default class ComponentChunk extends Component {
static RenderComponent = null;
state = { RenderComponent: ComponentChunk.RenderComponent };
componentWillMount() {
const { loadChunk } = this.props;
// Check if the component is not loaded already
if (!this.state.RenderComponent) {
loadChunk.then(module => module.default)
.then((component) => {
ComponentChunk.RenderComponent = component;
this.setState({ RenderComponent: component });
});
}
}
render() {
const { RenderComponent } = this.state;
const { componentProps } = this.props;
if (RenderComponent) {
return <RenderComponent {...componentProps} />;
}
return null;
}
}
ComponentChunk.propTypes = {
loadChunk: PropTypes.instanceOf(Promise).isRequired,
componentProps: PropTypes.object
};
ComponentChunk.defaultProps = {
componentProps: null
};