Skip to content

Commit 3d22e79

Browse files
author
Matteo Vesprini-Heidrich
committed
cleanup conditional detection of children type
1 parent e76202f commit 3d22e79

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

packages/react/src/ReactChildren.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,42 @@ function traverseAllChildrenImpl(
122122
) {
123123
var type = typeof children;
124124

125-
if (type === 'undefined' || type === 'boolean') {
126-
// All of the above are perceived as null.
127-
children = null;
128-
}
129-
130-
if (
131-
children === null ||
132-
type === 'string' ||
133-
type === 'number' ||
134-
// The following is inlined from ReactElement. This means we can optimize
135-
// some checks. React Fiber also inlines this logic for similar purposes.
136-
(type === 'object' && children.$$typeof === REACT_ELEMENT_TYPE) ||
137-
(type === 'object' && children.$$typeof === REACT_CALL_TYPE) ||
138-
(type === 'object' && children.$$typeof === REACT_RETURN_TYPE) ||
139-
(type === 'object' && children.$$typeof === REACT_PORTAL_TYPE)
140-
) {
125+
const invokeCallback = () => {
141126
callback(
142127
traverseContext,
143128
children,
144129
// If it's the only child, treat the name as if it was wrapped in an array
145130
// so that it's consistent if the number of children grows.
146131
nameSoFar === '' ? SEPARATOR + getComponentKey(children, 0) : nameSoFar,
147132
);
133+
};
134+
135+
if (type === 'undefined' || type === 'boolean') {
136+
// All of the above are perceived as null.
137+
children = null;
138+
}
139+
140+
if (children === null) {
141+
invokeCallback();
148142
return 1;
149143
}
150144

145+
switch (type) {
146+
case 'string':
147+
case 'number':
148+
invokeCallback();
149+
return 1;
150+
case 'object':
151+
switch (children.$$typeof) {
152+
case REACT_ELEMENT_TYPE:
153+
case REACT_CALL_TYPE:
154+
case REACT_RETURN_TYPE:
155+
case REACT_PORTAL_TYPE:
156+
invokeCallback();
157+
return 1;
158+
}
159+
}
160+
151161
var child;
152162
var nextName;
153163
var subtreeCount = 0; // Count of children found in the current subtree.

0 commit comments

Comments
 (0)