mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
24 lines
568 B
JavaScript
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} />
|
|
}
|
|
}
|
|
}
|