Created
September 9, 2017 07:17
-
-
Save milon87/fbb613dfeabf0a4575323ba6d9ef7e84 to your computer and use it in GitHub Desktop.
separating http call from ui logic and back to result
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
export function getvals(url){ | |
return fetch(url, | |
{ | |
method: "GET", | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
}) | |
.then((response) => response.json()) | |
.then((responseData) => { | |
return responseData; | |
}) | |
.catch(error => console.warn(error)); | |
} |
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, { Component } from 'react'; | |
import { | |
AppRegistry, | |
StyleSheet, | |
Text, | |
View | |
} from 'react-native'; | |
import {getvals} from './api'; | |
var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json'; | |
class Project extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
Total:0 | |
}; | |
} | |
componentDidMount() { | |
this.fetchData(); | |
} | |
fetchData() { | |
getvals(REQUEST_URL).then(responseData=>{ | |
this.setState({ Total: responseData.total}) | |
}) | |
} | |
render() { | |
return ( | |
<View style={styles.container}> | |
<Text style={styles.instructions}> | |
{this.state.Total} | |
</Text> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: 'center', | |
alignItems: 'center', | |
backgroundColor: '#F5FCFF', | |
}, | |
welcome: { | |
fontSize: 20, | |
textAlign: 'center', | |
margin: 10, | |
}, | |
instructions: { | |
textAlign: 'center', | |
color: '#333333', | |
marginBottom: 5, | |
}, | |
}); | |
AppRegistry.registerComponent('Project', () => Project); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment