Last active
January 5, 2019 16:46
-
-
Save andrzejewsky/8a245213e39d5daa4bb572cbbf043b4d to your computer and use it in GitHub Desktop.
Loading data
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 } from "react"; | |
import Loading from "./../components/Loading"; | |
import fetchData from "./../data"; | |
/* | |
variables - function that returns parameters (based on the component props) for the fetchData() | |
*/ | |
const withLoadingData = variables => WrappedComponent => { | |
class WithLoadingData extends Component { | |
state = { | |
loading: true, | |
response: null | |
}; | |
componentDidMount() { | |
const vars = variables ? variables(this.props) : null; | |
fetchData(vars).then(response => { | |
this.setState({ loading: false, response }); | |
}); | |
} | |
render() { | |
if (this.state.loading) { | |
return <Loading />; | |
} | |
return <WrappedComponent data={this.state.response} {...this.props} />; | |
} | |
} | |
return WithLoadingData; | |
}; | |
export default withLoadingData; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment