【发布时间】:2018-12-21 11:30:07
【问题描述】:
每次我运行代码时,它都会显示相同的错误。谁能解释这段代码有什么问题以及如何纠正它?项目在一个数组中,您可以在 url 'https://api.myjson.com/bins/8pyl4' 中查看它。
import React, { Component } from 'react';
import '../Stylesheet/bootstrap.min.css';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
items: [],
isLoaded: false
}
}
componentDidMount() {
fetch('https://api.myjson.com/bins/8pyl4')
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
items: json
})
});
}
render() {
var { isLoaded, items } = this.state;
if (!isLoaded) {
return <div>Loading...</div>;
}
return (
<div>
<ul>
{items.map(item => (
<li key="{item.ibn}">
Name: {item.name} , Author: {item.author}
</li>
))}
</ul>
</div>
);
}
}
【问题讨论】:
-
json 是一个对象,而不是一个数组,因此它没有 map 方法。