map

List<ReactElement>? map(

  1. JSAny? children,
  2. ReactElement fn(
    1. ReactElement child,
    2. int index
    )
)

Calls a function for each child in children.

If children is null or undefined, returns null. Otherwise, returns a new array with the results of calling fn on each child.

Example:

final items = Children.map(props['children'], (child, index) {
  return cloneElement(child, {'key': 'item-$index'});
});

Implementation

static List<ReactElement>? map(
  JSAny? children,
  ReactElement Function(ReactElement child, int index) fn,
) {
  ReactElement jsMapper(JSObject child, JSNumber index) =>
      fn(ReactElement.fromJS(child), index.toDartInt);

final result = _childrenMap(children, jsMapper.toJS); return result?.toDart .map( (e) => switch (e) { final JSObject o => ReactElement.fromJS(o), _ => throw StateError('Invalid child element'), }, ) .toList(); }