Skip to content

Commit

Permalink
cleanup conditional detection of children type
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Vesprini-Heidrich committed Nov 10, 2017
1 parent e76202f commit ba323c9
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions packages/react/src/ReactChildren.js
Expand Up @@ -122,29 +122,41 @@ function traverseAllChildrenImpl(
) {
var type = typeof children;

if (type === 'undefined' || type === 'boolean') {
// All of the above are perceived as null.
children = null;
}

if (
children === null ||
type === 'string' ||
type === 'number' ||
// The following is inlined from ReactElement. This means we can optimize
// some checks. React Fiber also inlines this logic for similar purposes.
(type === 'object' && children.$$typeof === REACT_ELEMENT_TYPE) ||
(type === 'object' && children.$$typeof === REACT_CALL_TYPE) ||
(type === 'object' && children.$$typeof === REACT_RETURN_TYPE) ||
(type === 'object' && children.$$typeof === REACT_PORTAL_TYPE)
) {
const invokeCallback = () => {
callback(
traverseContext,
children,
// If it's the only child, treat the name as if it was wrapped in an array
// so that it's consistent if the number of children grows.
nameSoFar === '' ? SEPARATOR + getComponentKey(children, 0) : nameSoFar,
);
};

switch (type) {
// All of the below are perceived as null.
case 'undefined':
case 'boolean':
children = null;
break;
case 'string':
case 'number':
invokeCallback();
return 1;
// The following is inlined from ReactElement. This means we can optimize
// some checks. React Fiber also inlines this logic for similar purposes.
case 'object':
switch (children.$$typeof) {
case REACT_ELEMENT_TYPE:
case REACT_CALL_TYPE:
case REACT_RETURN_TYPE:
case REACT_PORTAL_TYPE:
invokeCallback();
return 1;
}
}

if (children === null) {
invokeCallback();
return 1;
}

Expand Down

0 comments on commit ba323c9

Please sign in to comment.