Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React:在一个状态中循环通过一个数组_Javascript_Php_Jquery_Dictionary_Reactjs - Fatal编程技术网

Javascript 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

我正在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>
      )
    }
  });
我得到以下错误:

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: []
  };
}