Skip to content

Instantly share code, notes, and snippets.

@masiamj
Last active August 1, 2018 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masiamj/27b66c3fc965ccffa385f6fa5cd27120 to your computer and use it in GitHub Desktop.
Save masiamj/27b66c3fc965ccffa385f6fa5cd27120 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import GenericErrorComponent from './GenericErrorComponent'
import GenericLoadingComponent from './GenericLoadingComponent'
class GraphQLDataWrapper extends Component {
render () {
const { data, error, ErrorComponent, loading, LoadingComponent, renderData, ...rest } = this.props
// There was an error
if (error) return <ErrorComponent />
// Making the request
if (loading) return <LoadingComponent />
// Successful request!
return renderData({ data, ...rest })
}
}
GraphQLDataWrapper.propTypes = {
data: PropTypes.object,
error: PropTypes.object,
ErrorComponent: PropTypes.element.isRequired,
loading: PropTypes.bool.isRequired,
LoadingComponent: PropTypes.element.isRequired,
renderData: PropTypes.func.isRequired
}
GraphQLDataWrapper.defaultProps = {
ErrorComponent: () => <GenericErrorComponent />,
LoadingComponent: () => <GenericLoadingComponent />
}
export default GraphQLDataWrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment