我设法从API获取数据。现在我想在不点击的情况下从搜索输入中获取特定数据。我想我需要这里的过滤功能。我设法听取了点击的输入。但是如何获取数据呢?任何帮助将不胜感激。
这是我的代码
import React, { useState, useEffect } from "react";
import CountryListCard from "./CountryListCard";
import "./CountryList.scss";
export default function CountryList() {
const [data, setData] = useState([]);
const [search, setSearch] = useState("");
const fetchData = () => {
fetch("https://restcountries.eu/rest/v2/all")
.then((res) => res.json())
.then((result) => setData(result))
.catch((err) => console.log("error"));
};
useEffect(() => {
fetchData();
}, []);
function handleChange(e) {
console.log(e.target.value);
}
return (
<div>
<input
className="input"
type="text"
placeholder="search country ..."
value={search}
onChange={handleChange}
/>
{data &&
data.map((country) => (
<div className="CountryList" key={country.name}>
<div className="CountryListImg">
<img src={country.flag} alt="" width="80px" />
</div>
<div className="countryName">{country.name}</div>
<div className="population">{country.population}</div>
<div className="region">{country.region}</div>
<div>
{country.languages.map((language, languageIndex) => (
<div key={languageIndex}>{language.name}</div>
))}
</div>
</div>
))}
</div>
);
}
慕的地10843
沧海一幻觉
慕盖茨4494581
相关分类