reactjs - 警告:在循环调用 API 时遇到两个具有相同密钥的孩子?
问题描述
我是新手,我正在尝试通过 API 为主页调用一些文本,正在显示文本,但是浏览器控制台正在向“遇到两个具有相同密钥的孩子”错误发送垃圾邮件。此外,API 不断被调用,我将如何解决这个问题?
API
import axios from 'axios'
//BASE URL
const api = {
baseUrl: 'http://localhost:5029/'
}
export const Hentallehero = (alle) =>{
//http://localhost:5029/hero
let response = axios.get(api.baseUrl + 'hero')
.then(res => {
return res.data
})
.catch(error => {
console.log(error)
return null //vi svarer med null for at fortælle at noget gik galt = ingen data
})
// VIGTIGT - reunere data/null til component som "spørger"
console.log(response)
return response
}
家
import React from 'react'
import { useState, useEffect } from 'react'
import spa from '../../img/spa.png'
import leaf from '../../img/leaf.png'
// CSS
import '../../App.css'
// API
import { Hentallehero } from '../../helpers/apikald'
const Home = () => {
const [hero, setHero] = useState();
useEffect(() => {
Hentallehero("heroes").then((resonse) => setHero(resonse))
});
return (
<>
<img src={leaf} alt="" className="img-fluid" id="leaf" />
<img src={spa} alt="" className="img-fluid" id="spa" />
{hero?.map((item) => (
<div
className="container"
id=""
key={'alleboy'+item}
>
<div className="row">
<div className="col-lg-6 col-md-12 col-sm-12">
<p id="hero">{item?.titel1}</p>
<h1 id="herotitle" style={{ paddingLeft: "2.2em" }}>{item?.title2}</h1>
</div>
</div>
</div>
))}
</>
)
}
export default Home
解决方案
推荐阅读
- flutter - 通过 google_sign_in Flutter 刷新令牌获取新令牌
- python - 安装 pythonnet 时选项未识别错误
- r - 根据另一列在数据框中添加一列
- swift - 尽管检查了 nil,但使用 Firestore 在数组中填充数组时仍会崩溃
- .net-core - 在 .Net Core API 中处理嵌套的 JSON 请求数据
- amazon-web-services - 使用 api 密钥进行 AWS 负载均衡器运行状况检查
- c# - 如何在一帧中对多个游戏对象使用“OnClick.AddListener”
- c++ - 我的自定义字符串类 operator+ 有什么问题?
- python - 如何矢量化旅行推销员
- javascript - 与没有节点的 Babel 反应