【发布时间】:2020-04-22 10:41:40
【问题描述】:
我正在创建一个金融网站,但遇到了麻烦,因为 .map 没有在状态数组上执行。这两天我一直在敲墙。我在想这真的很小
这是我为填充数组和设置状态而进行的GET 调用。
.then(() => {
var transactions = [];
axios.get("http://localhost:5000/transactions").then(res => {
res.data.transactions.transactions.forEach(function(txn, idx) {
transactions.push({name: txn.name, amount: txn.amount, date: txn.date});
// item.push("hey");
});
});
this.setState({transactions: transactions, balance: content.balance});
}).catch(err => console.log(err));
那么我的渲染方法是这样的
render() {
var list = this.state.transactions.map((item, id) => {
return (
<VerticalTimelineElement
className="vertical-timeline-element--work"
contentStyle={{ background: 'rgb(33, 150, 243)', color: '#fff' }}
contentArrowStyle={{ borderRight: '7px solid rgb(33, 150, 243)' }}
date={item.date}
iconStyle={{ background: 'rgb(33, 150, 243)', color: '#fff' }}
key={id}
>
<h3 className="vertical-timeline-element-title">{item.name}</h3>
<h4 className="vertical-timeline-element-subtitle">${item.amount}</h4>
</VerticalTimelineElement>
);
});
return (
<VerticalTimeline>
{list}
</VerticalTimeline>
);
}
【问题讨论】:
-
你有什么问题?
-
没有任何东西被渲染,即使事务数组中有项目,map 方法也没有运行。
-
请展示地图最初的样子
-
我不明白你的意思。这是确切的代码。@cr05s19xx
-
另外,我注意到您没有在
.then块中更新您的状态。您应该可能将您的电话转移到axios.get().then()内的setState()
标签: javascript arrays reactjs web