Skip to content

Instantly share code, notes, and snippets.

@shalkam
Created June 19, 2018 22:07
Show Gist options
  • Save shalkam/d2404971429a3c991f52d16d42aed2bb to your computer and use it in GitHub Desktop.
Save shalkam/d2404971429a3c991f52d16d42aed2bb to your computer and use it in GitHub Desktop.
React loadable helper function
import loadable from './loadable';
const AsyncComponent = loadable(import('./my-component'));
import Loadable from 'react-loadable';
import React from 'react';
import Loading from './loading';
const loadable = (loader, render) => {
if (!render) {
// eslint-disable-next-line
render = (Loaded, props) => {
return <Loaded.default {...props} />;
};
}
return Loadable({
loader: () => loader,
render,
loading: Loading,
delay: 300,
timeout: 10000
});
};
export default loadable;
import React from 'react';
import PropTypes from 'prop-types';
const Loading = props => {
if (props.error) {
return <div>Error!</div>;
} else if (props.timedOut) {
return <div>Taking a long time</div>;
} else if (props.pastDelay) {
return <div>Loading ...</div>;
}
return null;
};
Loading.propTypes = {
error: PropTypes.object,
timedOut: PropTypes.bool,
pastDelay: PropTypes.bool
};
Loading.defaultProps = {
error: null,
timedOut: false,
pastDelay: false
};
export default Loading;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment