From ba323c97832f36b90890fec85640e3814d8d99a0 Mon Sep 17 00:00:00 2001 From: Matteo Vesprini-Heidrich Date: Fri, 10 Nov 2017 14:52:15 -0500 Subject: [PATCH] cleanup conditional detection of children type --- packages/react/src/ReactChildren.js | 44 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/react/src/ReactChildren.js b/packages/react/src/ReactChildren.js index fa8499830c1ed..f73f012333393 100644 --- a/packages/react/src/ReactChildren.js +++ b/packages/react/src/ReactChildren.js @@ -122,22 +122,7 @@ 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, @@ -145,6 +130,33 @@ function traverseAllChildrenImpl( // 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; }