Skip to content

Allow marking an array as "static" #3538

Closed
@glenjamin

Description

@glenjamin

I expected there to be an issue for this already, but couldn't find it. Apologies if this a dupe.

Occasionally it is useful to put a few elements in an array within render, when conditionally building different bits of the view.
React will issue a warning whenever an array is used that doesn't have keys - even if the author knows that this array is literal, and therefore doesn't require keys.

var label = "Label";
if (this.props.important) {
  label = ["Label", <Tag text="important" />];
}
return <div>
  <h3>{label}</h3>
  <p>{this.props.body}</p>
</div>

In this example, React will warn about lack of keys, even though they're not needed.

One option would be to extend React.addons.createFragment to accept an array, and use the indexes as keys.

label = React.addons.createFragment(["Label", <Tag text="important" />]);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions