Skip to content

Instantly share code, notes, and snippets.

@nguyenkiidu
Last active June 13, 2018 04:40
Show Gist options
  • Save nguyenkiidu/2d4a11ab2c8e4dc94243fd2f8dd8a365 to your computer and use it in GitHub Desktop.
Save nguyenkiidu/2d4a11ab2c8e4dc94243fd2f8dd8a365 to your computer and use it in GitHub Desktop.
class Bundle extends Component {
state = {
mod: null
}
componentWillMount() {
this.load(this.props)
}
componentWillReceiveProps(nextProps) {
if (nextProps.load !== this.props.load) {
this.load(nextProps)
}
}
load(props) {
this.setState({
mod: null
});
props.load().then(mod => {
this.setState({
// handle both es imports and cjs
mod: mod.default ? mod.default : mod
});
})
}
render() {
return this.state.mod ? this.props.children(this.state.mod) : null
}
}
const asyncComponent = (imp, props={}) =>
<Bundle
load={() => imp()}>
{Comp => <Comp {...props} />}
</Bundle>
const AsynPages = (props) => {
return asyncComponent(() => import('../containers/PagesContainer'), props)
}
<Route path='/pages' component={AsynPages} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment