Skip to content

Allow generators (that yield components) in place of arrays #7536

Closed
@mnpenner

Description

@mnpenner

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

Generators are silently discarded

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/reactjs/69z2wepo/).

This is allowed:

var Hello = React.createClass({
  render: function() {
    return <ul>
        {(() => [
        <li key="foo">foo</li>,
        <li key="bar">bar</li>
      ])()}
    </ul>;
  }
});

This should be legal too:

var Hello = React.createClass({
  render: function() {
    return <ul>
        {(function*() {
        yield (<li key="foo">foo</li>);
        yield (<li key="bar">bar</li>);
      })()}
    </ul>;
  }
});

Would be even better if the trailing () wasn't required, and it just executed the function.

What is the expected behavior?

Both examples should generate:

<ul><li>foo</li><li>bar</li></ul>

This would make it much easier to write if conditions in the middle of a JSX block. Here's a snippet of how this can be used from my current project:

 <div className="row">
     {generatorToArray(function*(){
         if(sameVehicleAndDriverForAllSegments) {
             yield <div key="billing_type" className="col">
                 <label>Billing Type</label>
                 <RadioGroup valueLink={linkState(billingForm, 'billing_type')}>
                     <RadioButton value="HOURLY">Hourly</RadioButton>
                     <RadioButton value="FLAT">Flat</RadioButton>
                 </RadioGroup>
             </div>;
         }

         switch(data.billing_type) {
             case 'HOURLY':
                 yield <div key="hourly_rate" className="col">
                     <label htmlFor="hourly_rate">Hourly Rate</label>
                     <div>$<TextBox id="hourly_rate" valueLink={linkState(billingForm, 'hourly_rate')} className="amount-input" disabled={!vehicleId}/>/hour</div>
                     <div><Slider {...hourlyRateSliderOptions} valueLink={linkState(billingForm, 'hourly_rate')} disabled={!vehicleId}/></div>
                 </div>;
                 break;
             case 'FLAT':
                 yield <div key="flat_rate" className="col">
                     <label htmlFor="flat_rate">Flat Rate</label>
                     <div>$<TextBox id="flat_rate" valueLink={linkState(billingForm, 'flat_rate')} className="amount-input" /></div>
                 </div>;
                 break;
         }
     })}
 </div>

Notice how I can write if conditions and switch statements without having to instantiate an empty array, push elements into it as needed, and then return an array at the end -- generators take care of all of that for you.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions