map
List<
- ReactElement child,
- 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();
}