Skip to content

Instantly share code, notes, and snippets.

@StevenLangbroek
Forked from RoyalIcing/1-loadable.js
Created November 19, 2015 12:50
Show Gist options
  • Save StevenLangbroek/cd6c68681fb78f1db0d8 to your computer and use it in GitHub Desktop.
Save StevenLangbroek/cd6c68681fb78f1db0d8 to your computer and use it in GitHub Desktop.
React Loadable Component
import React from 'react';
import Spinner from 'react-spinner';
export const loadable = (hasLoadedTest, Component) => props => hasLoadedTest(props) ? <Component { ...props } /> : <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 } />
}
*/
@RoyalIcing
Copy link

Nice one man!

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