Skip to content

Instantly share code, notes, and snippets.

@eugeniosegala
Last active October 6, 2018 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugeniosegala/8cda925193265e5b08940e743c332d85 to your computer and use it in GitHub Desktop.
Save eugeniosegala/8cda925193265e5b08940e743c332d85 to your computer and use it in GitHub Desktop.
// Async Component from Maximilian Schwarzmüller
import React, { Component } from 'react';
const asyncComponent = (importComponent) => {
return class extends Component {
state = {
component: null,
};
componentDidMount() {
importComponent()
.then(cmp => {
this.setState({ component: cmp.default });
});
}
render() {
const Chunk = this.state.component;
return Chunk ? <Chunk {...this.props} /> : null;
}
};
};
export default asyncComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment