简体   繁体   English

React JS Axios Response is not populating the array of objects returned by api. 响应返回了 20 个对象

[英]React JS Axios Response is not populating the array of objects returned by api. There are 20 objects returned by the response

在此处输入图像描述 hope you are doing well.希望你一切顺利。 Im just using ReactJS and I utilized the axios request to get news from a third party API. Now, the API returned 20 different articles.我只是使用 ReactJS,我利用 axios 请求从第三方 API 获取新闻。现在,API 返回了 20 篇不同的文章。 So I tried to use the map function to map over and iterate over the 20 articles and to create 20 cards(I created a card component that the api info would populate into).所以我尝试使用 map function 到 map 遍历 20 篇文章并创建 20 张卡片(我创建了一个卡片组件,api 信息将填充到其中)。 There are 20 articles, which means that there should be 20 cards.文章有20篇,也就是说应该有20张卡片。 However, only 1 card shows.但是,只显示 1 张卡片。 Please see below for my code.请参阅下面的代码。 Thanks!谢谢!

[![// TrumpNews
import React, { useEffect, useState } from "react";
import "./TrumpNews.css";
import CardComponent from "./CardComponent";
import axios from "axios";

const TrumpNews = () => {
  const API_KEY = "0949535b152f400aa2a162ecc055021a";
  const \[news, setNews\] = useState(\[\]);

  console.log(news);

  useEffect(() => {
    var url =
      "http://newsapi.org/v2/everything?" +
      "q=Trump&" +
      "from=2020-11-12&" +
      "sortBy=popularity&" +
      "apiKey=*********";

    axios
      .get(url)
      .then(async (res) => {
        setNews(\[res.data.articles\]);
      })
      .catch((err) => console.log(err));
  }, \[\]);

  return (
    <div className="trumpNews">
      {news.map((item) => (
        <CardComponent item={item} />
      ))}
    </div>
  );
};

export default TrumpNews;


// CardComponent
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Card from "@material-ui/core/Card";
import CardHeader from "@material-ui/core/CardHeader";

import IconButton from "@material-ui/core/IconButton";
import Typography from "@material-ui/core/Typography";
import { red } from "@material-ui/core/colors";
import FavoriteIcon from "@material-ui/icons/Favorite";
import ShareIcon from "@material-ui/icons/Share";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
import MoreVertIcon from "@material-ui/icons/MoreVert";
import "./CardComponent.css";

const useStyles = makeStyles((theme) => ({
  root: {
    maxWidth: 345,
  },
  media: {
    height: 0,
    paddingTop: "56.25%", // 16:9
  },
  expand: {
    transform: "rotate(0deg)",
    marginLeft: "auto",
    transition: theme.transitions.create("transform", {
      duration: theme.transitions.duration.shortest,
    }),
  },
  expandOpen: {
    transform: "rotate(180deg)",
  },
  avatar: {
    backgroundColor: red\[500\],
  },
}));

const CardComponent = ({ item }) => {
  const classes = useStyles();

  console.log(item.author);

  return (
    <Card className={classes.root}>
      <CardHeader
        action={
          <IconButton aria-label="settings">
            <MoreVertIcon />
          </IconButton>
        }
        title={item.description}
        subheader="September 14, 2016"
      />
      <br />
      <br />
      <br />
      <br />
      <br />
      <br />
      <br />
      <br />
      <br />
      <br />
      <div>{item\[0\].author}</div>
      {item\[0\].title}
    </Card>
  );
};

export default CardComponent;][1]][1]

You're creating a two dimensional array when you set the result -设置结果时,您正在创建一个二维数组 -

setNews([res.data.articles]);

articles is already an array, so you just need to set the state like this - articles已经是一个数组,所以你只需要像这样设置 state -

setNews(res.data.articles);

In the .then method in your example you are putting array in array, you do not need the square brackets here:在您示例的.then方法中,您将数组放入数组中,此处不需要方括号:

setNews(\[res.data.articles\]);

It should look like this:它应该是这样的:

setNews(res.data.articles);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM