Skip to content

Instantly share code, notes, and snippets.

@Bertrand31
Created June 14, 2017 03:41
Show Gist options
  • Save Bertrand31/c1da45a829f1fdd1ab2b199732e1b2f8 to your computer and use it in GitHub Desktop.
Save Bertrand31/c1da45a829f1fdd1ab2b199732e1b2f8 to your computer and use it in GitHub Desktop.
import React from 'react';
import { withApollo } from 'react-apollo';
import { logger } from '$utils';
export default (Component, queryOptions, queryAfterMount = true) => withApollo(
class extends React.Component {
constructor(props) {
super(props);
this.state = { data: { loading: true } };
}
componentDidMount() {
if (queryAfterMount) this.queryData();
}
queryData = () => {
this.props.client.query(queryOptions)
.then(({ data }) => this.setState({ data }))
.catch((err) => {
logger(err);
return this.setState({ data: { loading: false, error: true } });
});
}
cancelLoading = () => this.setState({ data: { loading: false } })
render = () => (
<Component
{...this.props}
data={this.state.data}
queryData={this.queryData}
cancelLoading={this.cancelLoading}
/>
);
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment