Last active
June 13, 2018 04:40
-
-
Save nguyenkiidu/2d4a11ab2c8e4dc94243fd2f8dd8a365 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
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