[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'this.state.result.map')]. I am getting this error whenever i am trying to fetch the data from the api. And also below in code i have used val.Global where Global is the title under which there is all the data. Can anyone tell what's the problem in my code?
import React from 'react';
import {View, Text, ActivityIndicator, StyleSheet, FlatList} from 'react-native';
export class mainScreen extends React.Component{
constructor(props){
super(props);
this.state = {
isloading: true,
result: []
}
}
componentDidMount(){
return fetch('https://api.covid19api.com/summary')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isloading: false,
result: responseJson.covid
})
})
}
render(){
if(this.state.isloading){
return (
<View style={styles.container}>
<ActivityIndicator/>
</View>
)
}else{
let covid = this.state.result.map((val, key) => {
return <View key={key} style={styles.item}><Text>{val.Global}</Text></View>
});
return(
<View style={styles.container}>
{covid}
</View>
);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
item: {
flex: 1,
alignSelf: 'stretch',
margin: 10,
alignItems: 'center',
justifyContent: 'center'
}
});