Skip to content

Instantly share code, notes, and snippets.

@ryanbrainard
Last active May 19, 2022 13:17
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanbrainard/788c12c3811d3da13124 to your computer and use it in GitHub Desktop.
Save ryanbrainard/788c12c3811d3da13124 to your computer and use it in GitHub Desktop.
Example PromiseStateContainer
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
@wangcongyi
Copy link

can i use this.setState() in onFulfillment()

@ryanbrainard
Copy link
Author

ryanbrainard commented Apr 25, 2017

@wangcongyi

can i use this.setState() in onFulfillment()

sure

@shrupa
Copy link

shrupa commented Oct 6, 2017

Hi,

I am getting-
Unhandled promise rejection TypeError: Invalid attempt to destructure non-iterable instance
on implementing onFulfillment

Could you please help?

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