Last active
November 19, 2015 12:51
-
-
Save RoyalIcing/a60029d22257291799cd to your computer and use it in GitHub Desktop.
React Loadable Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } /> | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turned it into a one-liner :) https://gist.github.com/StevenLangbroek/cd6c68681fb78f1db0d8