i createad basic weather app with react, and i want to all json file push the STATE but i can not do that.
import React, {Component} from 'react';
import './App.css';
import CityList from './CityList';
// this is an array that just has id, city_name and country property
import {cityName} from './Resorces';
class App extends Component {
constructor (){
super()
this.state = {
cities:{},
}
}
render() {
const {cities} = this.state;
console.log('cities state', cities);
return (
<div className='tc'>
<h1 className='f1 pa3 ma2'> World Weather</h1><hr/>
<CityList cities={cities}/>
</div>
);
}
componentDidMount(){
const citySource = cityName.map((city) => {
return fetch(https://api.weatherbit.io/v2.0/current?city=${city.name}&country=${city.country}&key=86e622607fbe4c2cb9f7f71889a4d48d)
}).then(response => response.json())
.then( data => {this.setState({cities : data})});
console.log('citySource', citySource);
}
}
export default App;
this code above dont access cityName array in Resorces.js and didnt execute map() loop and therefore didnt push json file to cities that a object in STATE
how can i do those?
and It is the cityName array in below
export const cityName = [
{
id: 1,
name: "New York City",
country: "US",
},
{
id: 2,
name: "Vienna",
country: "AT",
},
{
id: 3,
name: "Istanbul",
country: "TR",
},
{
id: 4,
name: "London",
country: "GB",
},
{
id: 5,
name: "Ankara",
country: "TR",
},
{
id: 6,
name: "Paris",
country: "FR",
},
{
id: 7,
name: "Madrid",
country: "ES",
},
{
id: 8,
name: "Amsterdam",
country: "NL",
},
{
id: 9,
name: "Belgrade",
country: "RS",
},
{
id: 10,
name: "Munich",
country: "DE",
},
{
id: 11,
name: "Berlin",
country: "DE",
},
{
id: 12,
name: "Chicago",
country: "US",
},
{
id: 13,
name: "Brussels",
country: "BE",
},
{
id: 14,
name: "Rome",
country: "IT",
},
{
id: 15,
name: "Washington",
country: "US",
}
];