Created
August 10, 2018 19:03
-
-
Save ninjaPixel/9a23f1f1b8ede9c9544d9ecd4f3b791b to your computer and use it in GitHub Desktop.
Defer the loading of a react component, in Meteor.js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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