javascript - 无法使用 array.map() 填充选项元素以选择元素
问题描述
我从 api 获取了一组车辆制造,并尝试将数组中的每个项目映射到选择元素中的选项元素:
const Makes = () => {
return (
<select aria-label="Select a make">
{/* Populate with option elements based on the years state and its index */}
<option value="">Select a make</option>
{makes.map((make) => <option key={make.toString()} value={make}>test</option>)}
</select>
);
};
makes
在上层范围内被定义为一个空数组,我用一个fetchMakesByYear
看起来像这样的函数填充数组:
const fetchMakesByYear = async () => {
const url = `https://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=${year}`;
const headers = {
headers: {
'Content-Type': 'application/xml',
Accept: 'application/xml',
},
};
try {
const data = await fetch(url, headers);
const itemUnformatted = await data.text();
const makesByYearUnformatted = convert.xml2js(itemUnformatted, { compact: true, spaces: 2 });
const makesByYear = makesByYearUnformatted.menuItems.menuItem;
for (let i = 0; i < makesByYear.length; i += 1) {
makes.push(makesByYear[i].text._text);
}
} catch (err) {
console.log(err);
}
};
useEffect(() => {
fetchMakesByYear();
});
不知道为什么没有填充选项。关于我在这里做错了什么的任何想法?谢谢。
解决方案
使用 state 来存储makes
数组并在 fetch 时更新它,这将重新渲染您的组件并useEffect
调用您fetchMakesByYear
的组件安装。
无限渲染是因为你没有传入依赖数组useEffect
,所以每次渲染都会调用它。
const [makes,setMakes] = useState([]) // use state to hold your array
const fetchMakesByYear = useCallback(async () => { // use useCallback hook to wrap your function so that this function is not created on every render and hence doesn't call useEffect as we will be giving it as a dependency
const url = `https://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=${year}`;
const headers = {
headers: {
'Content-Type': 'application/xml',
Accept: 'application/xml',
},
};
try {
const data = await fetch(url, headers);
const itemUnformatted = await data.text();
const makesByYearUnformatted = convert.xml2js(itemUnformatted, { compact: true, spaces: 2 });
const makesByYear = makesByYearUnformatted.menuItems.menuItem;
for (let i = 0; i < makesByYear.length; i += 1) {
makes.push(makesByYear[i].text._text);
}
setMakes(makes) // set state to update your array in the state
} catch (err) {
console.log(err);
}
},[]);
useEffect(()=>{
fetchMakesByYear()
},[fetchMakesByYear]) // add dependency in useEffect here
return (
{Makes()}
}
推荐阅读
- ansible - 如何在我的剧本上创建断言?
- java - 使用 java 以编程方式更新时,有没有办法保持文件的初始 yaml 格式?
- java - 将 HTML 转换为图像获取 java.io.IOException:流已关闭
- php - 逐行循环多维
- python-3.x - Keras 自定义损失 - 合并多个分支的损失
- python - 如何删除具有非 NaN 值的列的行
- reactjs - 反应复选框未初始获取对象的值
- cordova - 更改 Cordova Geolocation iOS 的显示名称
- git - GIT:查找补丁分支的连接点
- autodesk-forge - 我可以使用 Autodesk Forge 翻译包含内部组件(其他 .iam 文件)的 Inventor 文件 (.iam) 吗?