The only data I can access right now is the beginning part of the array:
[
{
/*start*/
"username" : "Bob",
"password":"123456",
"bio":"Hi",
/*End*/
"data":
[
{
"pet" : "dog",
"age" : "20",
"city" : "5"
},
{
"pet" : "cat",
"age" : "18",
"city" : "7"
}
]
}
]
I can also access part of the data
array, but I am trying to access all of it. For example: {item.data[1].pet}
. I tried using a for loop but was unsuccessful with that. I am also using react-native flat list and doing dataSource: responseJSON.data
didn't work for me either. I'm sure there has to be a way to access all of the values of pet
instead of just one each time, I just don't know how to.
Also since I am a beginner any tips will be gladly appreciated.
EDIT: Attempts at trying to loop from outside the JSX:
var itemList = [];
for (var i = 0; i < responseJSON[0].data.length; i++) { itemList.push(<Text> {responseJson[0].data[i].pet}</Text>); }
I put that out of the render()
Then I take itemList
and stick it here:
<FlatList
data={ this.state.dataSource}
ItemSeparatorComponent = {this.FlatListItemSeparator}
renderItem={({item}) => <View>
{itemList}
</View>
}
keyExtractor={(item, index) => index}
/>
Now it's not understanding that itemList is a variable. I'm getting a syntax error.
EDIT 2: Thanks to everyone the only thing I need help with is putting a variable inside of componentDidMount
in React Native.