Sfoglia il codice sorgente

HW34_React_CityCountry

HW34_React_CityCountry
Nastaliya 5 anni fa
parent
commit
4052e40868
1 ha cambiato i file con 71 aggiunte e 0 eliminazioni
  1. 71 0
      HW34_React_CityCountry/App.js

+ 71 - 0
HW34_React_CityCountry/App.js

@@ -0,0 +1,71 @@
+import React from 'react';
+import logo from './logo.svg';
+import './App.css';
+
+let CitySelect =cities=>{
+	
+	return <select>
+	{cities.cities}
+</select>
+}
+
+
+class CountrySelect extends React.Component{
+
+	constructor(props){
+		super(props)
+		this.state = {countries:{}, cities:[]};
+
+	}
+	
+	viewCities(country){
+		let cityList =[];
+		
+		for (let cityName of this.state.countries[country]){
+		cityList.push(<option value={cityName} key={cityList.length -1}>{cityName}</option>)
+		}
+		
+		return	this.setState({cities:cityList })
+	}
+	
+render(){
+
+	let list =[];
+	for (let countryName in this.state.countries){
+		
+	list.push(<option value={countryName} key={list.length -1}>{countryName}</option>)	
+	}
+	
+	return (
+	<div>
+	<select onChange = {evt=>{this.viewCities(evt.target.value)}}>
+	{list}
+	</select>
+	
+	<CitySelect cities={this.state.cities} />
+	</div>
+	)
+	
+}
+
+componentDidMount(){
+		fetch("https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json")
+
+	.then(res => res.json())
+
+	.then(data => {this.setState({countries:data });
+	
+	})
+	}
+	
+}
+
+function App() {
+  return (
+    <div className="App">
+     <CountrySelect />
+    </div>
+  );
+}
+
+export default App;