本文整理匯總了TypeScript中react.Children.toArray方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Children.toArray方法的具體用法?TypeScript Children.toArray怎麽用?TypeScript Children.toArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類react.Children
的用法示例。
在下文中一共展示了Children.toArray方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: cloneItem
export function cloneItem(
children: ReactNode,
props: { nativeEvent: TriggerEvent; propsFromTrigger?: object }
) {
return Children.map(
// remove null item
Children.toArray(children).filter(child => Boolean(child)),
item => cloneElement(item as ReactElement<any>, props)
);
}
示例2: findChildrenOfType
export function findChildrenOfType(children: React.ReactNode, type: any) {
let matchingChildren: React.ReactElement<any>[] = [];
let childrenToSearch = React.Children.toArray(children);
while (childrenToSearch.length > 0) {
let child = childrenToSearch.shift();
if (!IsReactElement(child)) {
continue;
} else {
if (child.type === type) {
matchingChildren.push(child);
}
let { props } = child;
if (props.children) {
// Children added to front of search array for DFS.
childrenToSearch.unshift(...React.Children.toArray(props.children));
}
}
}
return matchingChildren;
}
示例3:
const getKeys = (
children: React.ReactElement<any> | Array<React.ReactElement<any>>
): string[] =>
Children.toArray(children)
.filter(Boolean)
.map(getKey);