Skip to content
This repository was archived by the owner on Sep 13, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# CITY search lab is finished, but github wouldn't allow me to do git push. I will submit again later

# Zip Code React Lab

In this exercise, you will create two React apps:
Expand Down
60 changes: 54 additions & 6 deletions zip-search/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,74 @@ import React, { Component } from 'react';
import './App.css';


function City(props) {
return (<div>This is the City component</div>);
function City( {data} ) {
return (
<div>
City Name: { data.City }
<br />
State: { data.State }
<ul>
<li>Population: { data.EstimatedPopulation}</li>
</ul>
</div>
);
}



function ZipSearchField(props) {
return (<div>This is the ZipSearchField component</div>);
return (
/*
<div>
Zip Code:
<input type="text" onChange={ props.changeHandler }></input>
</div>
*/
<div>
<input onChange={ (e) => props.handleChange(e)} />
</div>
);
}


class App extends Component {

state = {
zipCode: '',
cities: [],
}

zipChange = (event) => {
this.setState({ zipCode: event.target.value })

if(event.target.value.length === 5){
fetch('https://ctp-zip-api.herokuapp.com/zip/'+event.target.value)
.then(res => res.json())
.then(cities => {
console.log(cities)
this.setState( { cities } );
})
.catch(err => {
this.setState({ cities:[] })
})
}else{
this.setState({ cities:[] })
}
}

render() {
return (
<div className="App">
<div className="App-header">
<h2>Zip Code Search</h2>
</div>
<ZipSearchField />
<ZipSearchField handleChange={ (e) => this.zipChange(e) }/>
<div>Current Zip code is: { this.state.zipCode }</div>
<div>
<City />
<City />
{ this.state.cities.length === 0 ? <h1>No Result</h1> : null }
{ this.state.cities.map(city => <City data={city} />) }


</div>
</div>
);
Expand Down