1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-dynamic-import/lib/with-import.js
2017-04-17 21:03:40 +05:30

24 lines
568 B
JavaScript

import React from 'react'
export default function withImport (promise, Loading = () => (<p>Loading...</p>)) {
return class Comp extends React.Component {
constructor (...args) {
super(...args)
this.state = { AsyncComponent: null }
}
componentDidMount () {
promise.then((AsyncComponent) => {
this.setState({ AsyncComponent })
})
}
render () {
const { AsyncComponent } = this.state
if (!AsyncComponent) return (<Loading {...this.props} />)
return <AsyncComponent {...this.props} />
}
}
}