Last active
July 25, 2018 08:12
-
-
Save selbekk/a553f4e883ded1e7b4b56c61a7d244c2 to your computer and use it in GitHub Desktop.
A boring ol' API backed container component
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
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