从React中打印来自Array的对象?
我想在React中使用新闻API制作新闻网站,但我无法显示JSON的文章 。 我认为获取功能是正确的,但在控制台中两次打印数据。我不知道为什么,如果您知道,请告诉我。另外,我无法使用阵列上的地图打印显示中的文章列表,请告诉我,我真的很困惑。
const [News, setNews] = useState([]);
useEffect(() => {
fetch(
`https://newsapi.org/v2/top-headlines?country=in&category=${category}&apiKey=${API_KEY}`
)
.then((response) => response.json())
.then((data) => {
console.log(data.articles);
setNews(data.articles);
});
}, [category]);
作为回报,我想获得文章列表,但这只是使我的桌面屏幕空白,甚至没有出现错误。
<ul>
{News.map((data) => {
<li key={data.title}>{data.title}</li>;
})}
</ul>
控制台返回
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {source: {…}, author: null, title: 'E-invoicing ‘soon to be mandated’ for units with over ₹10-cr turnover - BusinessLine', description: null, url: 'https://news.google.com/__i/rss/rd/articles/CBMikg…b3JlLW5leHQvYXJ0aWNsZTY1NTk0OTcyLmVjZS9hbXAv?oc=5', …}
1: {source: {…}, author: null, title: 'Cancer Horoscope for July 2022 - Susan Miller Astrology Zone - Astrology Zone', description: 'The latest in astrological trends by Susan Miller,…r advancement, travel, health, fitness, and more.', url: 'https://www.astrologyzone.com/forecasts/cancer-horoscope-for-july-2022/', …}
2: {source: {…}, author: 'Riya Ghosh', title: 'iPhone User? Know all about the WhatsApp Blur Tool - Techstory', description: 'This latest technology introduced by WhatsApp enab…ent of the picture before ', …}
3: {source: {…}, author: null, title: 'Supreme Court Judge Who Heard Nupur Sharma Plea Slams "Personal Attacks" - NDTV', descripti.....}
.....
请告诉我我做错了什么
I want to make a news website using news api in react but I am not able to display articles from json
.
I think fetch function is correct but it prints the data twice in console. I don't know why, please tell me about it if you know. Also, I am not able to print list of articles in display using map on array , please tell me , I am really confused.
const [News, setNews] = useState([]);
useEffect(() => {
fetch(
`https://newsapi.org/v2/top-headlines?country=in&category=${category}&apiKey=${API_KEY}`
)
.then((response) => response.json())
.then((data) => {
console.log(data.articles);
setNews(data.articles);
});
}, [category]);
In return body I wanted to get list of articles but it just makes my desktop screen blank , not even error shows up.
<ul>
{News.map((data) => {
<li key={data.title}>{data.title}</li>;
})}
</ul>
The console returns
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {source: {…}, author: null, title: 'E-invoicing ‘soon to be mandated’ for units with over ₹10-cr turnover - BusinessLine', description: null, url: 'https://news.google.com/__i/rss/rd/articles/CBMikg…b3JlLW5leHQvYXJ0aWNsZTY1NTk0OTcyLmVjZS9hbXAv?oc=5', …}
1: {source: {…}, author: null, title: 'Cancer Horoscope for July 2022 - Susan Miller Astrology Zone - Astrology Zone', description: 'The latest in astrological trends by Susan Miller,…r advancement, travel, health, fitness, and more.', url: 'https://www.astrologyzone.com/forecasts/cancer-horoscope-for-july-2022/', …}
2: {source: {…}, author: 'Riya Ghosh', title: 'iPhone User? Know all about the WhatsApp Blur Tool - Techstory', description: 'This latest technology introduced by WhatsApp enab…ent of the picture before ', …}
3: {source: {…}, author: null, title: 'Supreme Court Judge Who Heard Nupur Sharma Plea Slams "Personal Attacks" - NDTV', descripti.....}
.....
Please tell me what i did wrong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码中有一个错别字:
data..articles and data.Articals?
You have a typo in your code:
data.articles and data.articals ?
对于与控制台两次打印有关的问题,它可能与严格的模式有关,如果您使用的是React 18并且在开发模式下具有严格的模式,则在开发模式下运行两次,在生产模式下,它通常可以单一起作用。您可以阅读官方文档并查看 YouTube视频清楚地说明了为什么这样做。
对于第二个问题,为什么无法打印文章列表,它与News.map有关。在这里您缺少返回语句
让我知道这对您有所帮助。
For the issue related to printing twice in console, it might be related to Strict Mode, If you are using React 18 and have Strict mode on then in development mode it runs useEffect twice, in Production mode it will work normally that is single time. You can read the official documentation and check out the Youtube video which explain clearly why it was done so.
For the Second question why not able to print list of articles, it is related to News.map here you are missing return statement
Let me know does that helps.
问题1 :控制台打印两次
是因为使用exect 18的使用效率两次 - 请检查Chrome Dev工具上的“网络”选项卡以验证是否发送了两次请求。
您可以使用If/else语句,如果数据已经获取,则不要再次调用API。
Quality-2 :不显示列表
,因为您缺少映射功能中的返回语句。
希望这有帮助!
Problem-1 : Console is Printing Twice
That's because the useEffect ran twice if you are using React 18 - Please check the Network Tab on Chrome Dev Tools to verify if the request is sent twice.
You can use if/else statement, if the data is already fetched then don't call API again.
Problem-2 : Not Displaying List
That's becuase you are missing the return statement in the MAP function.
Hope this helps!