I' fetching data from axios through my API but when I'm rendering the data of it so it shows me blank template. So kindly let me know if I'm doing any mistake. I pasted my code with api response. Thanks
console.log => response.data
Data Array [
Object {
"ID": 2466,
"ItemName": "WWE2K20"
}
]
My Component
import React, { Component } from 'react';
import { Container, Header, Content, Card, CardItem, Body, Text } from 'native-base';
import { View, StyleSheet, FlatList, Image } from 'react-native';
import axios from 'axios';
export default class HomeScreen extends Component {
constructor() {
super()
this.state = {
itemList: []
}
}
async componentDidMount() {
await axios.get('myapiuri')
.then((response) => {
this.setState({
itemList: response.data
});
console.log("Data", this.state.itemList)
})
.catch(error=>console.log(error))
}
render() {
return (
<Container>
<Content>
<FlatList
data={this.state.itemList}
renderItem={(item) => {
return(
<Text>{item.ItemName}</Text>
);
}}
keyExtractor={(item) => item.ID}
/>
</Content>
</Container>
);
}
}
console.log("Data", this.state.itemList)to show state because setState is async function and next line can be executed even before actually setting state.