0

My problem is I get a list of data inside of my FetchData class buy its a list. I tried indexing the countries: data.Countries[0] for example 0 it gave me the values but I want all the data is there a way to do a foreach on this?

Fetch with index on countries: data.Countries[0] enter image description here

Fetch without index on countries: data.Countries enter image description here

fetchData.js


import React, { Component } from 'react';
import Countries from './countries';

class FetchData extends Component {
    
    state = {
        loading: true,
        countries: null
    };

    async componentDidMount() {
        const url = 'https://api.covid19api.com/summary';
        const response = await fetch(url);
        const data = await response.json();
        this.setState({
            countries: data.Countries,
            loading: false
        });
    }
     
  render() {
    return (
        <div>
            {this.state.loading || !this.state.countries ? ( 
            <div>loading</div>
            ) : (
            <div> 
                <Countries WorldCountries={this.state.countries.Country} /> 
            </div>
            )}
        </div>
    );
  }
}

export default FetchData;

countries.js

import React, { Component } from 'react'; 

const Countries = (prop) => {
    return(
    <h1>{prop.Countries}</h1>
    );
}
export default Countries;
2
  • You're passing "WorldCountries" as a prop but why in Countries component you are looking for "prop.Countries"? Commented Nov 9, 2020 at 21:14
  • @EmadEmami Oh my bad was trying something but i changed it back to prop.WorldCountries. Commented Nov 9, 2020 at 21:20

1 Answer 1

2

your images are confusing to me, but if I understood you correctly you need to do following:

  // FetchData component
  ...
  render() {
    return (
        <div>
            {this.state.loading || !this.state.countries ? ( 
            <div>loading</div>
            ) : (
            <div>
              {
                this.state.countries.map(country =>
                    <Country country={country.Country} />)
              }
            </div>
            )}
        </div>
    );
  }
  ...
  // FetchData component 


  // Country component
  const Countries = ({ country }) => (
    <h1>{country}</h1>
  );
  // Country component
Sign up to request clarification or add additional context in comments.

5 Comments

I get an error of TypeError: this.state.countries.map is not a function
@abdullahalhamidi what do you have in this.state.countries? what do you set to countries data.Countries[0] or data.Countries?
Multiple lists {"Country":"Albania","CountryCode":"AL","Slug":"albania","NewConfirmed":501,"TotalConfirmed":24206,"NewDeaths":2,"TotalDeaths":559,"NewRecovered":90,"TotalRecovered":12092,"Date":"2020-11-09T21:01:31Z","Premium":{}}
There are more lists per country
Oh it worked! I had to change the variable <Countries country={country.Country} />) it was <Countries country={country.Counttry} />) THANKS!

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.