Day_14 has been added

pull/64/head
asabeneh 4 years ago
parent d61d180469
commit 3b4e26e89b

1
.gitignore vendored

@ -1,5 +1,6 @@
draft.md draft.md
react-for-everyone.md react-for-everyone.md
component.md component.md
draft

@ -2,74 +2,67 @@ import React, { Component } from 'react'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
class App extends Component { class App extends Component {
firstName = React.createRef() constructor(props) {
lastName = React.createRef() super(props)
country = React.createRef() console.log('I am the constructor and I will be the first to run.')
title = React.createRef() this.state = {
firstName: 'John',
handleSubmit = (e) => { data: [],
// stops the default behavior of form element specifically refreshing of page
e.preventDefault()
console.log(this.firstName.current.value)
console.log(this.lastName.current.value)
console.log(this.title.current.value)
console.log(this.country.current.value)
const data = {
firstName: this.firstName.current.value,
lastName: this.lastName.current.value,
title: this.title.current.value,
country: this.country.current.value,
} }
// the is the place we connect backend api to send the data to the database
console.log(data)
} }
render() { componentDidMount() {
console.log('I am componentDidMount and I will be last to run.')
const API_URL = 'https://restcountries.eu/rest/v2/all'
fetch(API_URL)
.then((response) => {
return response.json()
})
.then((data) => {
console.log(data)
this.setState({
data,
})
})
.catch((error) => {
console.log(error)
})
}
renderCountries = () => {
return this.state.data.map((country) => {
const languageOrLanguages =
country.languages.length > 1 ? 'Langauges' : 'Language'
const formatLanguages = country.languages
.map(({ name }) => name)
.join(', ')
return ( return (
<div className='App'>
<h3>Add Student</h3>
<form onSubmit={this.handleSubmit}>
<div> <div>
<input
type='text'
name='firstName'
placeholder='First Name'
ref={this.firstName}
onChange={this.handleChange}
/>
</div>
<div> <div>
<input {' '}
type='text' <img src={country.flag} alt={country.name} />{' '}
name='lastName'
placeholder='Last Name'
ref={this.lastName}
onChange={this.handleChange}
/>
</div> </div>
<div> <div>
<input <h1>{country.name}</h1>
type='text' <p>Capital: {country.capital}</p>
name='country' <p>
placeholder='Country' {languageOrLanguages}: {formatLanguages}
ref={this.country} </p>
onChange={this.handleChange} <p>Population: {country.population}</p>
/>
</div> </div>
<div>
<input
type='text'
name='title'
placeholder='Title'
ref={this.title}
onChange={this.handleChange}
/>
</div> </div>
)
})
}
<button className='btn btn-success'>Submit</button> render() {
</form> return (
<div className='App'>
<h1>React Component Life Cycle</h1>
<h1>Calling API</h1>
<div>
<p>There are {this.state.data.length} countries in the api</p>
<div className='countries-wrapper'>{this.renderCountries()}</div>
</div>
</div> </div>
) )
} }

Loading…
Cancel
Save