Skip to content

Instantly share code, notes, and snippets.

@selbekk
Last active July 25, 2018 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selbekk/a553f4e883ded1e7b4b56c61a7d244c2 to your computer and use it in GitHub Desktop.
Save selbekk/a553f4e883ded1e7b4b56c61a7d244c2 to your computer and use it in GitHub Desktop.
A boring ol' API backed container component
class SomePage extends React.Component {
state = {
isPending: false,
error: null,
result: null,
};
async componentDidMount() {
this.setState({ isPending: true, error: null, result: null });
try {
const res = await fetch('/api/some-data');
const data = await res.json();
this.setState({ error: null, result: data });
} catch (e) {
this.setState({ error: e, result: null });
}
this.setState({ isPending: false });
}
render() {
// Renders the appropriate thing based on all of that state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment