0

I am trying to rendering the value from API, i can see array if i console the values, but not able to see it from Flatlist.

API fetch line is here

see this fetch line

i think this is where i am making mistakes

i think this is where i am making mistakes

console output array

console output array

Help me please,

2 Answers 2

2

Data returned from the backend is an array of objects. Refactor renderItem props as below.

<Flatlist
  keyExtractor={(item) => item.id}
  data={data}
  renderItem={({ item }) => (
    <>
      <Text>{item.date}</Text>
      <Text>{item.date_gmt}</Text>
    </>
  )}
/>

Sign up to request clarification or add additional context in comments.

Comments

0

Well, you are doing Two things wrong here. Let's start with the first one which is the loading part.

NOTE: I WILL BE USING YOUR EXISTING FLATLIST PROPS AS ...otherProps.

  1. Your are conditional rendering the whole Flatlist component, instead you can use ListEmptyComponent of flatlist to render loading or empty list views.

<Flatlist
  ...otherProps
  ListEmptyComponent={() => {
    if(isLoading){
      return <ActivityIndicator/>
    }
  }
/>

  1. Flatlist is a Pure Component, which means it needs a way to know if it should re-render, luckily we have an extraData prop which is for exactly this purpose. Taking from the previous example.
<Flatlist
  ...otherProps
  extraData={data}
  ListEmptyComponent={() => {
    if(isLoading){
      return <ActivityIndicator/>
    }
  }
/>

PS: you should really checkout the documentation for more information here https://reactnative.dev/docs/flatlist#listemptycomponent

1 Comment

this not what required. condition which i written is working. need to know how to read my array from flatlist, which is like this [{ id, title, ...}, { id, title, ...}, { id, title, ...},.....]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.