showPosts = async () => {
var userID = await AsyncStorage.getItem('userID');
fetch(strings.baseUri+"getPostWithUserID", {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"user_id": userID
})
})
.then((response) => response.json())
.then((responseJson) => {
let jsonObj = JSON.parse(JSON.stringify(responseJson));
if (jsonObj.status=="true") {
this.setState({
data: responseJson.data,
imageSrc: responseJson.data[0].imgSrc,
post_type: responseJson.data[0].post_type,
videoSrc: responseJson.data[0].video,
});
}
})
.catch((error) => {
console.error(error);
});
}
I'm fetching data from api and then using it in Flatlist.
This is what I get when I alert(responseJson.data)
.
Now, my responseJson.data[0].imgSrc
and responseJson.data[0].video
iterates each object and I'm able to display it on my screen via Flatlist. But this isn't the case in responseJson.data[0].post_type
.
responseJson.data[0].post_type
fetches only post_type
of first object from the JSON array. I've also added this.state.post_type
in extraData
.
Please tell me how to access data from Json array and then use it in Flatlist.
UPDATED CODE
<FlatList
data={this.state.data}
renderItem={ ({item,index}) => this._renderItem(item,index) }
extraData={[ this.state.data, this.state.checked, this.state.post_type ]}
/>
_renderItem = ( item, index ) => {
return(
{ item.post_type == "image" ?
<Image
//source={this.props.item.image}
source={image}}
//source={{ uri: strings.sourceUri+"userimage/"+item.imgSrc }}
style={styles.photo}
/>
:
<Video
source={video}
ref={(ref) => { this.player = ref }}
style={{ width: 300, height: 350, }}
/>
}
);
}