1

Hello i want to nest a list component inside a list component. The first list is an option group list, each of its items has a list of options. I think the problem comes from the nested IndicatorRow. I could'nt find a same example around.

var IndicatorRow = React.createClass({
 render: function() {

  var indicators = [];

  for (var key in this.props.indicatorsOfGroup) {
      indicators.push( <option value={key} > {this.props.indicatorsOfGroup[key]}</option> );
  }

  return {indicators} ;
 }
});

var IndicatorGroup = React.createClass({
render : function(){

  var indicatorsGroup = [];
  for (var key in this.props.indicatorsGroups) {

      var indicatorsOfGroup = this.props.indicatorsGroups[key].indicators;

      indicatorsGroup.push( 
         <optgroup label={key}> 
             <IndicatorRow indicatorsOfGroup={indicatorsOfGroup}/>
         </optgroup> );

  }

    return ( <select> 
                {indicatorsGroup} 
            </select> );
}
});

ReactDOM.render(
    <form>
        <fieldset className="form-group">
            <IndicatorGroup indicatorsGroups={indicatorsSelected}/>
        </fieldset>
    </form>
 ,
 document.getElementById('indicators')
);

My data is structured as below

var indicatorsSelected = {
"Economy & Growth": {
    "indicators": {
        "id1": "Indicator 1",
        "id2": "Indicator 2"
    }
},
"Energy & Mining": {
    "indicators": {
        "id1": "Indicator 1",
        "id1": "Indicator 2"
    }
},
"Environment": {
    "indicators": {
        "id1": "Indicator 1",
        "id2": "Indicator 2"
    }
},
{...}
};

I would appreciate any help

1 Answer 1

1

2 problems:

  1. You cannot return an Array from render
  2. Your Array children each need a key attribute

something like:

var IndicatorRow = React.createClass({
  render: function() {
    var indicators = [];

    for (var key in this.props.indicatorsOfGroup) {
      indicators.push( <option key={key} value={key} > {this.props.indicatorsOfGroup[key]}</option> );
    }

    return (<optgroup  label={key}>
      {indicators}
    </optgroup>);
  }
});

var IndicatorGroup = React.createClass({
  render: function(){
    var indicatorsGroup = [];
    for (var key in this.props.indicatorsGroups) {
      var indicatorsOfGroup = this.props.indicatorsGroups[key].indicators;

      indicatorsGroup.push( 
        <IndicatorRow key={key} indicatorsOfGroup={indicatorsOfGroup}/>
      );

    }
    return (<form>
      <fieldset className="form-group">
        <select> 
          {indicatorsGroup} 
        </select>             
      </fieldset>
    </form>);
  }

});

ReactDOM.render(
<IndicatorGroup indicatorsGroups={indicatorsSelected}/>,
 document.getElementById('indicators')
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.