Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not access 3rd level of json for id

i have a flatlist in my app and loading data from json api. Now having this issue that i can not access id of a booking in flatlist keyExtractor. that is why nothing is showing in my flatlist.
Json file:

{
    "success": true,
    "data": {
        "bookings": [
            {
                "id": 1,
                "customer_id": 1,
                "driver_id": 1,
                "width": 1,
                "height": 1
            }
        ]
    },
    "message": "Current booking list"
}

i can access success like this : BookingData.success but i dont know how to pass "id" in keyExtractor to load data in flatlist.

<FlatList
  data={BookingData}
  renderItem={renderItem}
  keyExtractor={(item, index) => item.data.bookings[index].id}
/>

right now flatlist is not showing any data.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to pass an array as data:

<FlatList
  data={BookingData.data.bookings}
  renderItem={renderItem}
/>

The keyExtractor prop is not needed, as FlatList checks if the items have an id attribute already.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading