Created
June 20, 2017 02:18
-
-
Save alexhawkins/93bc822b3a4cbc1e17a6ad5c8b4f39ca to your computer and use it in GitHub Desktop.
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
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 Pure Component { | |
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 { ip } = await res.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