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() {
return ( console.log('I am componentDidMount and I will be last to run.')
<div className='App'> const API_URL = 'https://restcountries.eu/rest/v2/all'
<h3>Add Student</h3> fetch(API_URL)
<form onSubmit={this.handleSubmit}> .then((response) => {
<div> return response.json()
<input })
type='text' .then((data) => {
name='firstName' console.log(data)
placeholder='First Name' this.setState({
ref={this.firstName} data,
onChange={this.handleChange} })
/> })
</div> .catch((error) => {
<div> console.log(error)
<input })
type='text' }
name='lastName' renderCountries = () => {
placeholder='Last Name' return this.state.data.map((country) => {
ref={this.lastName} const languageOrLanguages =
onChange={this.handleChange} country.languages.length > 1 ? 'Langauges' : 'Language'
/> const formatLanguages = country.languages
</div> .map(({ name }) => name)
.join(', ')
return (
<div>
<div> <div>
<input {' '}
type='text' <img src={country.flag} alt={country.name} />{' '}
name='country'
placeholder='Country'
ref={this.country}
onChange={this.handleChange}
/>
</div> </div>
<div> <div>
<input <h1>{country.name}</h1>
type='text' <p>Capital: {country.capital}</p>
name='title' <p>
placeholder='Title' {languageOrLanguages}: {formatLanguages}
ref={this.title} </p>
onChange={this.handleChange} <p>Population: {country.population}</p>
/>
</div> </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