Created
February 11, 2017 14:21
-
-
Save pozylon/7e7a6d9744a8bb4fd211985319049823 to your computer and use it in GitHub Desktop.
React & Recompose & Meteor dynamic-import
This file contains hidden or 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
import { compose, withState, lifecycle } from 'recompose'; | |
let AsyncComponent = null; | |
export default (loader, loadingComponent) => compose( | |
withState('isDynamicallyLoaded', 'updatedIsDynamicallyLoaded', false), | |
lifecycle({ | |
componentDidMount() { | |
const { updatedIsDynamicallyLoaded } = this.props; | |
loader.then((LoadedModule) => { | |
AsyncComponent = LoadedModule; | |
updatedIsDynamicallyLoaded(true); | |
}).catch((e) => { | |
console.error('Error while trying to dynamically load a react component: ' + e); // eslint-disable-line | |
}); | |
}, | |
}))( | |
({ isDynamicallyLoaded, | |
updatedIsDynamicallyLoaded, | |
...rest | |
}) => ( | |
(AsyncComponent && isDynamicallyLoaded && updatedIsDynamicallyLoaded) ? | |
AsyncComponent.default(rest) : | |
loadingComponent(rest))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment