Skip to content

Instantly share code, notes, and snippets.

@RoyalIcing
Last active November 19, 2015 12:51
Show Gist options
  • Save RoyalIcing/a60029d22257291799cd to your computer and use it in GitHub Desktop.
Save RoyalIcing/a60029d22257291799cd to your computer and use it in GitHub Desktop.
React Loadable Component
import React from 'react';
import Spinner from 'react-spinner';
export default function loadable(hasLoadedTest, Component) {
return (props) => {
if (hasLoadedTest(props)) {
return <Component { ...props } />;
}
else {
return <Spinner />;
}
}
}
// Will only render if `items` is present, otherwise renders a spinner
const LoadableList = loadable(
({ items }) => !!items,
List // List is a React component
);
/*
render() {
return <LoadableList items={ this.state.items } />
}
*/
// Will only render if `image` is present, otherwise renders a spinner
const LoadableImage = loadable(
({ image }) => !!image,
({ image: { URL, width, height }, description }) =>
<img src={ URL } width={ width } height={ height } alt={ description } />
);
/*
render() {
return <LoadableImage image={ this.state.image } />
}
*/
@StevenLangbroek
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment