【发布时间】:2017-10-17 14:33:58
【问题描述】:
我正在尝试遍历 axios 调用“/Home/NewsNotes”返回的对象。
我可以在屏幕上显示属性名称,但在访问“NewsNotes”数组时遇到问题。
这是我的组件代码。
class ReleaseDetailsComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
releaseNotes: {}
};
}
componentDidMount() {
var _this = this;
const urlParams = new URLSearchParams(window.location.search);
const currCatId = urlParams.get('categoryId');
axios.get('/Home/NewsNotes?categoryId=' + currCatId)
.then(response => _this.setState(
{ releaseNotes: response.data }
))
.catch(function (error) {
console.log(error);
});
console.log(currCatId);
}
render() {
return (
<section>
<div className="row">
<div className="col-sm-8 col-sm-offset-2 col-md-10 col-md-offset-1">
<h2 className="page-title tutorials"><img className="title-icon" src="/Content/images/icons/icon-release-notes.png" /> News & Release Notes</h2>
<h3>{this.state.releaseNotes.Title}</h3>
{Object.keys(this.state.releaseNotes).map(function (item, key) {
return (
<p>{item}</p>
);
})}
</div>
</div>
</section>
);
}
}
ReactDOM.render(
<ReleaseDetailsComponent />,
document.getElementById('tutorialsWrapper')
);
【问题讨论】:
-
使用
this.state.releaseNotes.NewsNotes访问它
标签: arrays reactjs asp.net-mvc-4