Created
December 29, 2016 19:19
-
-
Save laphilosophia/db9aab9a31ae861a13a2bae27a9d3e69 to your computer and use it in GitHub Desktop.
Async Await with React Lifecycle methods
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 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> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source: http://bit.ly/2hRuBOb