Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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