-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
42 lines (37 loc) · 1.29 KB
/
App.js
File metadata and controls
42 lines (37 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React, { useState, useEffect } from 'react';
import SearchBar from './components/SearchBar';
import CountryList from './components/CountryList';
const App = () => {
const [countries, setCountries] = useState([]);
const [filteredCountries, setFilteredCountries] = useState([]);
const [suggestions, setSuggestions] = useState([]);
useEffect(() => {
fetch('https://dpaste.com/79QXDY8TD')
.then((response) => response.json())
.then((data) => {
const countryData = data.map((country) => ({
name: country.name.common,
capital: country.capital ? country.capital[0] : 'No Capital',
}));
setCountries(countryData);
setFilteredCountries(countryData);
setSuggestions(countryData.map((country) => country.name));
});
}, []);
const handleSearch = (query) => {
const result = countries.filter(
(country) =>
country.name.toLowerCase().includes(query.toLowerCase()) ||
country.capital.toLowerCase().includes(query.toLowerCase())
);
setFilteredCountries(result);
};
return (
<div className="app">
<h1>Country Search</h1>
<SearchBar onSearch={handleSearch} suggestions={suggestions} />
<CountryList countries={filteredCountries} />
</div>
);
};
export default App;