Skip to content

Instantly share code, notes, and snippets.

@ninjaPixel
Created August 10, 2018 19:03
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 ninjaPixel/9a23f1f1b8ede9c9544d9ecd4f3b791b to your computer and use it in GitHub Desktop.
Save ninjaPixel/9a23f1f1b8ede9c9544d9ecd4f3b791b to your computer and use it in GitHub Desktop.
Defer the loading of a react component, in Meteor.js.
import React from 'react';
class DeferredComponent extends React.Component {
constructor(props) {
super(props);
this.state = {Component: null, loading: true};
}
componentDidMount() {
this.loadComponent();
}
loadComponent() {
this.props.importFunction().then((Component) => {
this.setState({loading: false, Component: Component.default});
});
}
render() {
const props = this.props;
const {loading, Component} = this.state;
if (loading) {
return this.props.loadingComponent || null
}
return (<Component {...props} />);
}
}
export default DeferredComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment