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
Changes from 1 commit
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
Next Next commit
withSlots: avoid calling traverseAllChildren twice
  • Loading branch information
KevinTCoughlin committed Oct 12, 2020

Verified

This commit was signed with the committer’s verified signature. The key has expired.
miri64 Martine Lenders
commit f3de7f92516304826961cf7a305f7eec7c35e5fc
13 changes: 6 additions & 7 deletions packages/foundation/src/slots.tsx
Original file line number Diff line number Diff line change
@@ -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?
@@ -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.