Skip to content

Instantly share code, notes, and snippets.

@andrzejewsky
Last active January 5, 2019 16:46
Show Gist options
  • Save andrzejewsky/8a245213e39d5daa4bb572cbbf043b4d to your computer and use it in GitHub Desktop.
Save andrzejewsky/8a245213e39d5daa4bb572cbbf043b4d to your computer and use it in GitHub Desktop.
Loading data
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