0

I am trying to call data in the react app from fake Postman API, and so far it works well when I call from a single array of objects:

{
   "animals": [
 {
      "id": 1,
      "name": "Tiger",
      "diet": "Carnivore",
      "habitant": "Asia",
      "lfespan": "1-15 years"
 },
  {
      "id": 2,
      "name": "Lion",
      "diet": "Carnivore",
      "habitant": "Africa",
      "lfespan": "1-15 years"
  },
 ]
}

App.js

class App extends React.Component {
  state = {
   animals: []
}
// arrow function - no need to bind
getAnimal = async (e) => {
 const animalName = e.target.elements.animalName.value;
   e.preventDefault();
   const api_call = await fetch(`https://4ab06793-05d5-47b8-b3cb- 
   56c26a95e5d4.mock.pstmn.io/animals`)

 const data = await api_call.json();
   this.setState({animals: data.animals});
    console.log(this.state.animals)
}

However, my data does not display on the screen when I try to call the data from categories: wildCats, birds. I just don't know how to do it. I am pretty sure this is super-easy, but I am a beginner and I could not find the answer anywhere. What I tried to do, and it did not work out:

 App.js

 class App extends React.Component {
 state = {
  animals: [
   {
     wildCats: []
   }
  ]
 }
 // arrow function - no need to bind
 getAnimal = async (e) => {
 const animalName = e.target.elements.animalName.value;
 e.preventDefault();
 const api_call = await fetch(`https://4ab06793-05d5-47b8-b3cb- 
 56c26a95e5d4.mock.pstmn.io/animals`)

 const data = await api_call.json();
 this.setState({animals: data.animals.wildCats});
 console.log(this.state.animals.wildCats)
 }

JSON file

{
 "animals": [
 { 
  "wildCats": [
    {
      "id": 1,
      "name": "Tiger",
      "diet": "Carnivore",
      "habitant": "Asia",
      "lfespan": "1-15 years"
    },
    {
      "id": 2,
      "name": "Lion",
      "diet": "Carnivore",
      "habitant": "Africa",
      "lfespan": "1-13 years"
    },
    {
      "id": 3,
      "name": "Leopard",
      "diet": "Carnivore",
      "habitant": "Africa, South America",
      "lfespan": "1-12 years"
    }
  ],
  "birds": [
    {
      "id": 4,
      "name": "Macaw",
      "diet": "Herbivore",
      "habitant": "South America",
      "lfespan": "1-70 years"
    },
   ]
  }
 ]
}
2
  • Are you still wanting to store the entire animals array in state? Your setState will likely mutate your state object shape. Commented Nov 1, 2020 at 20:01
  • What are you using animalName for in the getAnimal callback? What is your expected result of fetching an animal/animals? It isn't clear if you are trying to render the nested array data, or if you are trying to make a fetch request based on the nested array data. Please include a Minimal, Complete, and Reproducible code example, please include the entire component code. Commented Nov 1, 2020 at 20:10

1 Answer 1

2

Try this, as data.animals is an data.animalsArray, you should getting the data with index.

 const data = await api_call.json();
 this.setState({ animals: data.animals[0].wildCats });

 or 
 this.setState({ animals: data.animals[0].birds });

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

1 Comment

So wipe out any other existing animal categories? I don't think this is what the OP is asking for.

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.