I'm trying to use fetch api in react native using following code snippet. Whenever I try to console.log the API data array it always comes up 'undefined'?!?!
Home.JS
import api from '../components/api';
class Home extends Component {
constructor(props){
super(props);
this.state = {
profile: []
}
}
componentWillMount(){
api.getProfile().then((res) => {
this.setState({
profile: res.profile
})
});
}
render() {
console.log("Profile: ", this.state.profile); // Console Out API Data
return (
<View style={styles.container}>
<Text style={styles.welcome}></Text>
</View>
);
}
};
API.JS
var api = {
getProfile(){
var url = 'https://api.lootbox.eu/xbl/us/Food%20Market/profile';
return fetch(url).then((res) => res.json());
}
};
module.exports = api;
console.logtheres.datainstead ofres.profile