【发布时间】:2018-06-04 21:43:16
【问题描述】:
我正在尝试在 React.js 前端显示来自本地 API 的数据。
它显示了一个只有表格标题的空白表格,但表格上没有条目。 JSON 来自 API,就好像我 console.log(this.state.cards) 它在命令行上记录数组一样。
感谢任何帮助!
谢谢!
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;
【问题讨论】: