asyncawaitreact.jsx

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> ); } }
Async Await with React Lifecycle methods

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.