Skip to content

Accept iterables and collections as children. #2296

@leebyron

Description

@leebyron

React currently accepts Arrays as children:

var array = [<span />, <span />, <span />];
<div>{spans}</div>

It would be excellent if we can generalize this concept to accept both collections and iterables.

Collections (such as Set, Map, or Immutable-js's variants) can be identified if they implement values() and the result of calling values() is an Iterator (typeof maybeIterator.next === 'function').

Iterables (Array, arguments object, other array-ish things, third-party data structures) can be identified if they have Symbol.iterator or @@iterator returning an Iterator.

In both cases, you're left with an Iterator, at which point extracting the children becomes straight-forward.

This unlocks a few really useful things:

Use Immutable-js with React efficiently:

var data = Immutable.Vector('a', 'b', 'c');
<div>{data.map(str => <span>{str.toUpperCase}</span>)}</div>

Use generators:

var data = function*() {
  yield <span />;
  yield <span />;
  yield <span />;
}
<div>{data}</div>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions