这是一个功能组件.在这里,我使用useEffect钩子来实现API对搜索的依赖性.我正在做控制台的地方.log(${search}
)正在控制台中显示当前搜索.如何在数组中获取最后五个搜索,然后显示它们?
const [city, setCity] = useState(null);
const [search, setSearch] = useState("Dehradun");
useEffect ( () => {
const fetchApi = async () => {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${search}&units=metric&appid=7938d9005e68d8b258a109c716436c91`
const response = await fetch(url);
fetch("https://api.openweathermap.org/data/2.5/weather?q=${search}&units=metric&appid=7938d9005e68d8b258a109c716436c91")
.then(result => console.log(`${search}`, result))
const resJson = await response.json();
setCity(resJson.main);
};
fetchApi();
},[search] )```