Skip to content

Instantly share code, notes, and snippets.

@saiyerniakhil
Last active June 30, 2019 14:23
Show Gist options
  • Save saiyerniakhil/a7a5a18c1aab76c9dc413ec04788b3a4 to your computer and use it in GitHub Desktop.
Save saiyerniakhil/a7a5a18c1aab76c9dc413ec04788b3a4 to your computer and use it in GitHub Desktop.
import React,{useEffect, useState} from 'react';
import axios from 'axios';
// import logo from './logo.svg';
import './App.css';
const App = () => {
const [countriesData,setCountriesData] = useState([])
const [country, setCountry] = useState([])
const [foundCountry,setFoundCountry] = useState([])
const hook = () => {
axios.get("https://restcountries.eu/rest/v2/all")
.then(response => {
console.log("data fetched")
setCountriesData(countriesData => countriesData.concat(response.data))
console.log(countriesData)
})
}
useEffect(hook,[])
const handleSubmit = (e) => {
e.preventDefault()
}
const findCountry = (val) => {
var data = countriesData.filter(d => {
return d.name === val
})
return data
}
const handleInputChange = (e) => {
setCountry(e.target.value)
let country = (e.target.value).toLowerCase()
const x = findCountry(country)
console.log(x)
}
return (
<div>
<form onSubmit={handleSubmit}>
<input type="text" value={country} onChange={handleInputChange}/>
<button type="submit">find</button>
</form>
<h3> </h3>
</div>
)
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment