Skip to content

Instantly share code, notes, and snippets.

@pozylon
Created February 11, 2017 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pozylon/7e7a6d9744a8bb4fd211985319049823 to your computer and use it in GitHub Desktop.
Save pozylon/7e7a6d9744a8bb4fd211985319049823 to your computer and use it in GitHub Desktop.
React & Recompose & Meteor dynamic-import
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