Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
Minor cleanups and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
swernerx committed Mar 9, 2017
1 parent da3c6f2 commit 4e75493
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/common/createLazyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,37 @@ export default function createLazyComponent(options)
super(props)

this.state = {
loading: LazyLoading,
component: LazyComponent,
messages: LazyMessages
loading: LazyLoading
}
}

fetchData() {
if (LazyLoading === true || LazyComponent != null) {
return LazyPromise
componentDidMount()
{
// This callback is only executed on the client side
// We have to manually call fetchData there as this is our own infrastructure for
// loading data and typically not being executed on the client by the normal React render flow.
if (!LazyPromise) {
this.fetchData()
}
}

console.log("LazyComponent: Loading...", options.id)
this.setState({
loading: true
})
fetchData()
{
if (this.state.loading === false && LazyComponent == null)
{
// Toggle loading state even when this is not the initating instance
this.setState(() => ({
loading: true
}))
}

// Return existing promise from some other initiator... or when called two times in a row.
if (LazyPromise != null) {
return LazyPromise
}

LazyLoading = true
LazyPromise = Promise.all(options.load(this.props.language)).then((result) => {
console.log("LazyComponent: Done!", options.id)

LazyLoading = false

// Map component. We have to use the default export, because import() does not
Expand All @@ -47,25 +58,15 @@ export default function createLazyComponent(options)
LazyMessages = result[1]

this.setState({
loading: LazyLoading,
component: LazyComponent,
messages: LazyMessages
loading: LazyLoading
})
})

return LazyPromise
}

componentDidMount() {
// This callback is only executed on the client side
// We have to manually call fetchData there as this is our own infrastructure for
// loading data and typically not being executed on the client by the normal React render flow.
if (!LazyPromise) {
this.fetchData()
}
}

render() {
render()
{
if (!LazyComponent) {
return null
}
Expand All @@ -82,7 +83,8 @@ export default function createLazyComponent(options)
}
}

LazyComponentWrapper.propTypes = {
LazyComponentWrapper.propTypes =
{
children: React.PropTypes.node,
locale: React.PropTypes.string,
language: React.PropTypes.string,
Expand Down

0 comments on commit 4e75493

Please sign in to comment.