javascript - 前端导师休息国家 API
问题描述
我目前正在开发一个由 frontendmentor.io 提供的名为 Rest Country API 的项目。
在这里,我试图获得一个国家,并在边界数组中的 3 个字母代码的帮助下,我试图获得边境国家的名称。但它们不会出现在浏览器中。任何人都可以帮忙吗?我错过了什么?提前致谢
import React, { useEffect, useState } from "react";
import { Link, useParams } from "react-router-dom";
import { useGlobalState } from "../context";
const url = "https://restcountries.eu/rest/v2/name/";
const url2 = "https://restcountries.eu/rest/v2/alpha/";
function Country() {
const { countryName } = useParams();
const [isLoading, setIsLoading] = useState(true);
const [countryBorders, setCountryBorders] = useState([]);
const [country, setCountry] = useState(null);
const { isDarkMode } = useGlobalState();
const fetchingSingleCountry = async () => {
let newBorders = [];
const response = await fetch(url + countryName);
const data = await response.json();
const borders = data[0].borders;
borders.map(async (border) => {
const response = await fetch(url2 + border);
const data = await response.json();
newBorders.push(data.name);
setCountryBorders(newBorders);
});
setCountryBorders(newBorders);
setCountry(data[0]);
setIsLoading(false);
};
useEffect(() => {
fetchingSingleCountry();
}, []);
if (isLoading) {
return (
<div className="title-center">
<h1>Loading</h1>
</div>
);
}
const {
flag,
name,
nativeName,
population,
region,
subregion,
capital,
topLevelDomain,
currencies,
languages,
} = country;
return (
<div className="single-country">
<div className="single-country-img">
<img src={flag} alt={name} />
</div>
<div className="single-country-info">
<h1>{name}</h1>
<div>
<div className="info-left">
<p>
<span className="text-info">native name: </span>
{nativeName}
</p>
<p>
<span className="text-info">population: </span>
{population}
</p>
<p>
<span className="text-info">region: </span>
{region}
</p>
<p>
<span className="text-info">sub region: </span>
{subregion}
</p>
<p>
<span className="text-info">capital: </span>
{capital}
</p>
</div>
<div className="info-right">
<p>
<span className="text-info">top level domain: </span>
{topLevelDomain.join(",")}
</p>
<p>
<span className="text-info">currencies: </span>
{currencies.map((currency, index) => {
return (
<>
<span key={index}>{currency.name}</span>
<span
className={`${
index === currencies.length - 1 ? "not-show" : ""
}`}
>
,{" "}
</span>
</>
);
})}
</p>
<p>
<span className="text-info">languages: </span>
{languages.map((language, index) => {
return (
<>
<span key={index}>{language.name}</span>
<span
className={`${
index === languages.length - 1 ? "not-show" : ""
}`}
>
,{" "}
</span>
</>
);
})}
</p>
</div>
</div>
<div className="border-countries">
<p>Borders</p>
<div>
{countryBorders.map((border, index) => {
return <p>{border}</p>;
})}
</div>
</div>
</div>
</div>
);
}
export default Country;
解决方案
您的问题是您设置了setCountryBorders(newBorders);
两次,为您创建了一个沙箱。
推荐阅读
- ios - 为什么我收到错误消息自定义字体,反应原生?
- javascript - 为什么每当我使用不同的用户登录时,我都会看到相同的购物车信息。每个用户都应该不同
- c# - 正则表达式匹配分隔符
- mysql - #1170 - 密钥规范中使用的 BLOB/TEXT 列“question_content”没有密钥长度
- python - 仅根据训练时满足条件的样本更新keras模型的权重
- python - Docker hyperkit 进程 CPU 使用率变得疯狂。如何控制它?
- actions-on-google - 谷歌助手的谷歌登录 /// 不知道如何编码
- python - 如何计算从开始到的整数个数
- python-3.x - 如何划分两个聚合数据框
- gcc - 我可以使用“no_caller_saved_registers”或“interrupt”属性来编写任务切换过程吗?