如何正確地顯示列表通過URL收到JSON?如何顯示列表?
這裏是一個project的一個例子。如果我使用局部變量 - 一切正常,如果我得到列表,它會顯示一個錯誤。
constructor(props) {
super(props);
this.urlNews = "https://api.myjson.com/bins/1nrbo";
this.state = {
news: "",
links: [
{
name: "Имя 1",
url: "http://localhost:1",
use: true
},
{
name: "Имя 2",
url: "http://localhost:2",
use: false
},
{
name: "Имя 3",
url: "http://localhost:3",
use: false
}
]
};
}
getNews() {
fetch(this.urlNews)
.then(response => {
return response.json();
})
.then(json => {
this.setState({
news: json
});
console.log(this.state.news[1]);
});
}
componentDidMount() {
this.getNews();
}
render() {
const { news } = this.state;
return (
<ul>
{news.map((item, index) => <li>1</li>)}
</ul>
);
}
怎麼回事?
而且它顯示什麼樣的錯誤?當你在這裏尋求幫助時,你應該總是這樣說。 – Meier