Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active June 20, 2017 02:24
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 alexhawkins/8a129990e0d0227dd5c9653fbf2d580c to your computer and use it in GitHub Desktop.
Save alexhawkins/8a129990e0d0227dd5c9653fbf2d580c to your computer and use it in GitHub Desktop.
import React, { PureComponent } from 'react'
const contextTypes = {
router: PropTypes.object.isRequired,
api: PropTypes.object.isRequired,
relay: PropTypes.shape({
variables: PropTypes.shape({
category: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
}
class AsyncAwait extends PureComponent {
constructor(props, context) {
super(props, context)
this.state = {}
}
async componentDidMount() {
const res = await this.context.api.fetch('/logout', { method: 'POST' }).then(() => {
this.context.api.environment.getStore().getSource().clear();
this.context.router.push(location.pathname);
});
const ipResponse = await fetch('https://api.ipify.org?format=json')
const { ip } = await ipResponse.json()
await this.setStateAsync({ ipAddress: ip })
}
setStateAsync(state) {
return new Promise((resolve) => {
this.setState(state, resolve)
})
}
render() {
return (
<div
My IP is {this.state.ipAddress || 'Unknown'}
</div>
)
}
}
AsyncAwait.contextTypes = contextTypes;
export default AsyncAwait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment