Last active
October 6, 2018 09:51
-
-
Save eugeniosegala/8cda925193265e5b08940e743c332d85 to your computer and use it in GitHub Desktop.
This file contains 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
// 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