Javascript React:在一个状态中循环通过一个数组
我正在React中构建一个组件。在我尝试通过一个状态循环之前,一切似乎都很正常 以下是我的组件:Javascript React:在一个状态中循环通过一个数组,javascript,php,jquery,dictionary,reactjs,Javascript,Php,Jquery,Dictionary,Reactjs,我正在React中构建一个组件。在我尝试通过一个状态循环之前,一切似乎都很正常 以下是我的组件: var BidItem = React.createClass({ getInitialState: function() { return { theMaxBid: '', theHighestBids: '' }; }, componentDidMount: function() { var $compo
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>
)
}
});
我得到以下错误:
Uncaught TypeError: cc.map is not a function
但当我在下面的dd数组中循环时,它是有效的:
{dd.map(function(result){
console.log(result);
})}
为什么我不能循环状态数组
为什么我不能循环状态数组
因为你没有数组<代码>最高出价:'创建一个空字符串
改变
getInitialState: function() {
return {
theMaxBid: '',
theHighestBids: ''
};
}
到
并确保
响应。最高出价也是一个数组。函数组件didmount
get在第一次渲染调用后运行,因此初始渲染不会有此.state。最高出价
(提示:最高出价
)。第一次渲染是在该.state下运行的。最高出价
返回'
,它没有#map
功能
将getInitialState
更改为highestbid:[]
,它将在第一次映射到一个空数组,然后在组件装载时调用AJAX,然后您将得到一个响应,该响应将填充第二次呈现的状态
getInitialState: function() {
return {
theMaxBid: '',
theHighestBids: ''
};
}
getInitialState: function() {
return {
theMaxBid: '',
theHighestBids: []
};
}