Javascript 渲染函数中的返回变量

Javascript 渲染函数中的返回变量,javascript,jquery,reactjs,Javascript,Jquery,Reactjs,如何在react component render函数中返回变量, 我尝试输出列表,并且在循环期间必须在wrap/container标记属性中打印一些数据 var WFilterOptionListLevel0 = React.createClass({ loadOptionLevel0: function() { $.ajax({ url: this.props.getOption, data: {"level":0}, type: 'POST'

如何在react component render函数中返回变量, 我尝试输出列表,并且在循环期间必须在wrap/container标记属性中打印一些数据

var WFilterOptionListLevel0 = React.createClass({
  loadOptionLevel0: function() {
    $.ajax({
      url: this.props.getOption,
      data: {"level":0},
      type: 'POST',
      dataType: 'json',
      success: function(data) {
        this.setState({optionLevel0: data.option.list});
      }.bind(this),
    });
  },

  getInitialState: function() {
    return {optionLevel0: []};
  },
  componentDidMount: function() {
    this.loadOptionLevel0Mock();
    this.loadOptionLevel0();
  },
  render: function() {
    var optionList = this.state.optionLevel0.map(function(o) {
      return (
        <div className="option-list level-0" data-id={o.id}>
          <div className="title handle-slide-list-container">{o.title}</div>
        </div>
      );
    });
    return (
      {optionList}
    );
  }
});

React组件应该只返回中的单个根节点,但您当前的实现正在返回一个数组。您需要将输出包装在单个节点中,如:


您似乎应该在this.state.optionLevel0而不是this.props.getOption上运行map。您当前的实现实际上返回了一个数组,可能是语法错误,或者如果启用了ES6语法,则是一个具有属性optionList的对象。感谢您的澄清,@FelixKling。我没有试着运行它。
render: function() {
  var optionList = this.state.optionLevel0.map(function(o) {
    return (
      <div className="option-list level-0" data-id={o.id}>
        <div className="title handle-slide-list-container">{o.title}</div>
      </div>
    );
  });
  return (
    <div>
      {optionList}
    </div>
  );
}