Skip to content

Instantly share code, notes, and snippets.

@bafonins
Created July 22, 2019 13:48
Show Gist options
  • Select an option

  • Save bafonins/e5738ac23bb0aff2ce1e144a4c3a536a to your computer and use it in GitHub Desktop.

Select an option

Save bafonins/e5738ac23bb0aff2ce1e144a4c3a536a to your computer and use it in GitHub Desktop.
import React from 'react'
import Spinner from '../../components/spinner'
const withRequest = (
isFetchingTest = () => false,
isUpdatedTest = () => false
) => Component =>
class WithRequest extends React.Component {
componentDidMount () {
const { loadData } = this.props
loadData()
}
componentDidUpdate (prevProps) {
const { error, loadData } = this.props
if (Boolean(error) && prevProps.error !== error) {
throw error
}
if (isUpdatedTest(this.props)) {
loadData()
}
}
render () {
if (isFetchingTest(this.props)) {
return <Spinner center />
}
const { error, loadData, ...rest } = this.props
return <Component {...rest} />
}
}
export default withRequest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment