为什么在尝试显示get request的内容时仅在.map中显示的第一个值?
我已经尝试了所有我可能可以做的一切,但由于某种原因,仅数组中的第一个索引([{...},{...},{...}],{...}]
)显示在浏览器上 - 其他两个指数被忽略。我已经搜索了这个问题,因此发现人们面临的一些类似问题,但他们的解决方案都不适合我。
我在做什么错?我该如何制作,以便浏览器上显示数组中的所有内容?
这是我的代码:
import React, {useEffect, useState} from "react";
import '../../../sass/prizes/prizes.scss';
const Prizes = () => {
const [prizeData, setPrizeData] = useState([]);
useEffect(() => {
axios.get('http://localhost/api/prizes')
.then(resp => {
setPrizeData([resp]);
console.log(prizeData)
}).catch(error => {
console.log(error);
});
}, []);
return (
<div className="main">
<h1>Prizes</h1>
<ul className="cards">
<li className="cards_item">
<div className="card">
{
prizeData.map((prizes, index) => {
return(
<>
<div className="card_image"><img src={prizes.data[index].image_url} className="giftCards"/></div>
<div className="card_content">
<h2 className="card_title">{prizes.data[index].prizeName}</h2>
</div>
</>
);
})
}
</div>
</li>
</ul>
</div>
);
};
export default Prizes;
I've tried everything I possibly could but for some reason, only the first index in the array ([{...}, {...}, {...}]
) is being displayed on the browser - the other two indices are being ignored. I've searched this problem up on SO and found some similar issues people were facing but none of their solutions worked for me.
What am I doing wrong? How can I make it so that all contents in the array are being shown on the browser?
Here's my code:
import React, {useEffect, useState} from "react";
import '../../../sass/prizes/prizes.scss';
const Prizes = () => {
const [prizeData, setPrizeData] = useState([]);
useEffect(() => {
axios.get('http://localhost/api/prizes')
.then(resp => {
setPrizeData([resp]);
console.log(prizeData)
}).catch(error => {
console.log(error);
});
}, []);
return (
<div className="main">
<h1>Prizes</h1>
<ul className="cards">
<li className="cards_item">
<div className="card">
{
prizeData.map((prizes, index) => {
return(
<>
<div className="card_image"><img src={prizes.data[index].image_url} className="giftCards"/></div>
<div className="card_content">
<h2 className="card_title">{prizes.data[index].prizeName}</h2>
</div>
</>
);
})
}
</div>
</li>
</ul>
</div>
);
};
export default Prizes;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
某些表现是正确的,应该是setPriedata(resp)。如果您获得prizedata.map不是一个函数,那么响应(resp)可能有问题。
resp只是一个数组吗?您可以打印状态以查看其中存储的内容吗?
CertainPerformance is right, it should be setPrizeData(resp). If you get prizeData.map is not a function then maybe there's something wrong with the response (resp).
Is resp just an array? Can you print the state to see what's stored in it?
当您这样做时,您基本上将Prop用作数组:
这就是为什么您只会获得单个索引,因为您将其用作数组。
而是这样做:
您遇到的错误:
API可能正在返回对象。做一个卷曲请求或一些事情以查看您的响应的样子。
如果您可以确认实际获取数据,请在Axios中尝试使用类似的内容:
此外,这是您可以使用的模板:
You're basically using a prop as an array when you do this:
This is why you only get a single index because you are using it as an array.
Instead do this:
The error you're getting:
The API is probably returning an object. Do a CURL request or something to see what your response looks like.
If you can confirm you're actually getting data, try something like this in your Axios get:
Additionally, here is a template you can use: