Last active
May 19, 2022 13:17
-
-
Save ryanbrainard/788c12c3811d3da13124 to your computer and use it in GitHub Desktop.
Example PromiseStateContainer
This file contains hidden or 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, { Component, PropTypes } from 'react' | |
import { connect, PromiseState } from 'react-refetch' | |
import LoadingAnimation from './LoadingAnimation' | |
import ErrorBox from './ErrorBox' | |
class PromiseStateContainer extends Component { | |
static propTypes = { | |
ps: PropTypes.instanceOf(PromiseState).isRequired, | |
onPending: PropTypes.func, | |
onNoResults: PropTypes.func, | |
onRejection: PropTypes.func, | |
onFulfillment: PropTypes.func.isRequired, | |
} | |
static defaultProps = { | |
onPending: (meta) => <LoadingAnimation/>, | |
onNoResults: (value, meta) => <ErrorBox error="No results"/>, | |
onRejection: (reason, meta) => <ErrorBox error={reason}/>, | |
} | |
render() { | |
const { ps, onPending, onNoResults, onRejection, onFulfillment } = this.props | |
if (ps.pending) { | |
return onPending(ps.meta) | |
} else if (ps.rejected) { | |
return onRejection(ps.reason, ps.meta) | |
} else if (ps.fulfilled && $.isEmptyObject(ps.value)) { | |
return onNoResults(ps.value, ps.meta) | |
} else if (ps.fulfilled) { | |
return onFulfillment(ps.value, ps.meta) | |
} else { | |
console.log('invalid promise state', ps) | |
return null | |
} | |
} | |
} | |
export default PromiseStateContainer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am getting-
Unhandled promise rejection TypeError: Invalid attempt to destructure non-iterable instance
on implementing onFulfillment
Could you please help?