0

I am trying to fetch JSON data from this API: 'https://dog.ceo/api/breeds/list/all' I have tried several times even from official docs, but nothing helped , i am not getting error but nthing shows onn screen

 useEffect(() => {
    fetch('https://dog.ceo/api/breeds/list/all')
      .then((response) => response.json())
      .then((json) => setData(json.message))
      .catch((error) => console.error(error))
      .finally(() => setLoading(false));
  }, []);

   
  return (
    <View style={{ flex: 1, padding: 24 }}>
      {isLoading ? <ActivityIndicator/> : (
        <FlatList
          data={data}
          keyExtractor={({ id }, index) => id}
          renderItem={({ item }) => (
            <Text>{item.message}  ghbdt </Text>
                      )}
        />
      )}
    </View>

2 Answers 2

1

you can try using async and call handleData on an event click or using useEffect as well. Hope this way helps you.

async handleData() {
axios.get('https://dog.ceo/api/breeds/list/all')
  .then((response) => response.json())
  .then((json) => setData(json.message))
  .catch((error) => console.error(error))
  .finally(() => setLoading(false));
  }

Another way maybe you can try passing data in array dependency useEffect[].

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

1 Comment

sorry but where should i put it??
0

The problem you are hitting is the return from the API is an object not an array:

{"message":{"affenpinscher":[],"african":[],"airedale":[]...}}

To make this work for you you will need to turn the returned data in to an array, if all you are after is the key value and not the values in the arrays then you could just change your setData to setData(Object.keys(json.message)); this will then give you a list of dogs out.

Comments

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.