Created
July 22, 2019 13:48
-
-
Save bafonins/e5738ac23bb0aff2ce1e144a4c3a536a to your computer and use it in GitHub Desktop.
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 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