首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法在React.js上显示API数据

无法在React.js上显示API数据
EN

Stack Overflow用户
提问于 2018-06-05 05:43:16
回答 1查看 95关注 0票数 0

我正在尝试在React.js前端显示来自本地API的数据。

它显示一个只有表头的空表,但表上没有条目。JSON从API中通过,就好像I console.log(this.state.cards),它在命令行上记录数组。

如有任何帮助,我们不胜感激!

谢谢!

代码语言:javascript
复制
import React, { Component } from 'react';
import '../css/card.css';
import Card from './Card';
import CardTable from './CardTable.js';
import request from 'request';

class TopCards extends Component {

    constructor() {
        super();
        this.state = {
          cards: [],
        };
      }

    componentDidMount() {
        const url = "http://localhost:1200";
        request({
            url: url,
            json: true
        }, (error, response, body) => {
            if (!error && response.statusCode === 200) {
                for (let i in body) {
                    this.state.cards.push(body[i]);
                }
            }
        })
    }

    render() {


        if(this.state.cards) {
            var cardList = this.state.cards.map(function(card){
                return(
                    <CardTable key={card.cardName} card={card} />
                );
            });
        }

        return (
            <div className="TopCards">
                <h1>Top Cards This Week:</h1>
                <br />
                <table width="400">
                <tr>
                        <th>Name</th>
                        <th>Count</th>
                        <th>Wins</th>
                        <th>Losses</th>
                        <th>Win %</th>
                </tr>
                    {cardList}
                </table>
            </div>
        )
    }
}

export default TopCards;
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50689184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档