我有一个由10个对象组成的数组(我们称之为“博客”),其中包含标题,描述和图像URL属性。我需要将每个属性都用HTML标签包装起来,并将它们全部导出,以便它们都一起在网页上加载。
通过我的当前代码,我只能在当前状态下加载1个对象到页面。如何获取所有对象处于相同状态?
通过我的当前代码,我只能在当前状态下加载1个对象到页面。如何获取所有对象处于相同状态?
class NewBlogs extends React.Component {
constructor(props) {
this.state = {
title: [],
description: [],
image: [],
loading: true
};
}
componentDidMount() {
axios.get('/new-blogs').then(data => {
const blogs = data.data;
var component = this;
for(var i in blogs) {
component.setState({
title: blogs[i].title,
description: blogs[i].description,
image: blogs[i].image,
loading: false
});
}
})
.catch(function(error) {
console.log(error);
});
}
render() {
return (
<div>
<h2>New Blogs:</h2>
<h3>{this.state.title}</h3>
<em>{this.state.description}</em>
<img src={this.state.image}></img>
</div>
);
}
}
export default NewBlogs