react-with-loading-srian is a react HOC that shows your data after it's been fetched, a loader when it's loading, and an error if it fails.
npm i react-with-loading-hoc
const View = ({ id, title, completed }) => (
<div>
<div>ID: {id}</div>
<div>Title: {title}</div>
<div>Completed? {completed ? 'Yes' : 'No'}</div>
</div>
);
const ViewWithLoading = withLoading(View);
<ViewWithLoading loading={!state} {...state} />
withLoading.propTypes = {
Component: PropTypes.element,
FallbackOptions: PropTypes.shape({ // FallbackOptions allows you to write your own HOC with your own default Fallbacks
LoadingFallback: PropTypes.func,
ErrorFallback: PropTypes.func,
}),
};
Wrapped.propTypes = {
loading: PropTypes.bool.isRequired,
error: PropTypes.any,
LoadingFallback: PropTypes.func,
ErrorFallback: PropTypes.func,
};