【发布时间】:2016-05-14 18:27:12
【问题描述】:
我正在 React 中构建一个组件。在我尝试循环一个状态之前,一切似乎都运行良好。
这是我的组件:
var BidItem = React.createClass({
getInitialState: function() {
return {
theMaxBid: '',
theHighestBids: ''
};
},
componentDidMount: function() {
var $component = this;
$.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "get_max_bid"},
})
.done(
function(response){
$component.setState({
theMaxBid: response.max_bid,
theHighestBids: response.highest_bids
});
}
);
},
render: function() {
var dd = [{ids:"2"}, {ids:"5"}];
var cc = this.state.theHighestBids;
console.log(cc);
console.log(dd);
return (
<div>
<p>Max Bid: {this.state.theMaxBid}</p>
</div>
)
}
});
这可行,并且在渲染函数中 cc 和 dd 都输出一个如下所示的数组:
当我在渲染函数中循环遍历 cc 数组(来自状态)时:
{cc.map(function(result){
console.log(result);
})}
我收到以下错误:
Uncaught TypeError: cc.map is not a function
但是当我遍历下面的 dd 数组时,它可以工作:
{dd.map(function(result){
console.log(result);
})}
为什么我不能循环状态数组?
【问题讨论】:
标签: javascript php jquery dictionary reactjs