react-app-shell
Version:
react打包脚本和example, 这里的版本请忽略
33 lines (24 loc) • 607 B
JavaScript
import React, { Component } from 'react';
function asyncComponent(importComponent) {
class AsyncComponent extends Component {
state = {
component: null
};
async componentDidMount() {
const { default: component } = await importComponent();
!this.isCancelled &&
this.setState({
component: component
});
}
componentWillUnmount() {
this.isCancelled = true;
}
render() {
const C = this.state.component;
return C ? <C {...this.props} /> : null;
}
}
return AsyncComponent;
}
export default asyncComponent;