Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.0] withSlots: refactor out redundant React.Children traversal #15471

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "patch",
"comment": "withSlots: avoid calling traverseAllChildren twice",
"packageName": "@uifabric/foundation",
"email": "KevinTCoughlin@users.noreply.github.com",
"dependentChangeType": "patch",
"date": "2020-10-12T15:20:41.946Z"
}
13 changes: 6 additions & 7 deletions packages/foundation/src/slots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ export function withSlots<P>(
): ReturnType<React.FunctionComponent<P>> {
const slotType = type as ISlot<P>;
if (slotType.isSlot) {
// TODO: There is something weird going on here with children embedded in props vs. rest args.
// Comment out these lines to see. Make sure this function is doing the right things.
const numChildren = React.Children.count(children);
if (numChildren === 0) {
return slotType(props);
}

// Since we are bypassing createElement, use React.Children.toArray to make sure children are
// properly assigned keys.
// TODO: should this be mutating? does React mutate children subprop with createElement?
Expand All @@ -56,6 +49,12 @@ export function withSlots<P>(
// Is there a better way to prevent slots from appearing in hierarchy? toArray doesn't address root issue.
children = React.Children.toArray(children);

// TODO: There is something weird going on here with children embedded in props vs. rest args.
// Comment out these lines to see. Make sure this function is doing the right things.
if (children.length === 0) {
return slotType(props);
}

return slotType({ ...(props as any), children });
} else {
// TODO: Are there some cases where children should NOT be spread? Also, spreading reraises perf question.
Expand Down