Skip to content

Instantly share code, notes, and snippets.

@laphilosophia
Created December 29, 2016 19:19
Show Gist options
  • Save laphilosophia/db9aab9a31ae861a13a2bae27a9d3e69 to your computer and use it in GitHub Desktop.
Save laphilosophia/db9aab9a31ae861a13a2bae27a9d3e69 to your computer and use it in GitHub Desktop.
Async Await with React Lifecycle methods
class AwesomeProject extends Component {
state = {}
setStateAsync(state) {
return new Promise((resolve) => {
this.setState(state, resolve)
});
}
async componentDidMount() {
StatusBar.setNetworkActivityIndicatorVisible(true)
const res = await fetch('https://api.ipify.org?format=json')
const {ip} = await res.json()
await this.setStateAsync({ipAddress: ip})
StatusBar.setNetworkActivityIndicatorVisible(false)
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
My IP is {this.state.ipAddress || 'Unknown'}
</Text>
</View>
);
}
}
@laphilosophia
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment