From eafe0e4277ad39b6f0d84cc1322058756494a0aa Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Fri, 9 Apr 2021 17:31:08 -0400 Subject: [PATCH] Merge isObject branches We assume that isArray and getIteratorFn are only called on objects. So we shouldn't have to check that again and again, and then check a flag. We can just stay in this branch. There is a slight semantic breakage here because you could have an iterator on a function, such as if it's a generator function. But that's not supported and that currently only works at the root. The inner slots don't support this. So this just makes it consistent. --- .../src/ReactChildFiber.new.js | 46 +++++++++---------- .../src/ReactChildFiber.old.js | 46 +++++++++---------- 2 files changed, 42 insertions(+), 50 deletions(-) diff --git a/packages/react-reconciler/src/ReactChildFiber.new.js b/packages/react-reconciler/src/ReactChildFiber.new.js index e9fdf0b654b5a..024a903dbd915 100644 --- a/packages/react-reconciler/src/ReactChildFiber.new.js +++ b/packages/react-reconciler/src/ReactChildFiber.new.js @@ -1231,9 +1231,7 @@ function ChildReconciler(shouldTrackSideEffects) { } // Handle object types - const isObject = typeof newChild === 'object' && newChild !== null; - - if (isObject) { + if (typeof newChild === 'object' && newChild !== null) { switch (newChild.$$typeof) { case REACT_ELEMENT_TYPE: return placeSingleChild( @@ -1266,6 +1264,26 @@ function ChildReconciler(shouldTrackSideEffects) { ); } } + + if (isArray(newChild)) { + return reconcileChildrenArray( + returnFiber, + currentFirstChild, + newChild, + lanes, + ); + } + + if (getIteratorFn(newChild)) { + return reconcileChildrenIterator( + returnFiber, + currentFirstChild, + newChild, + lanes, + ); + } + + throwOnInvalidObjectType(returnFiber, newChild); } if (typeof newChild === 'string' || typeof newChild === 'number') { @@ -1279,28 +1297,6 @@ function ChildReconciler(shouldTrackSideEffects) { ); } - if (isArray(newChild)) { - return reconcileChildrenArray( - returnFiber, - currentFirstChild, - newChild, - lanes, - ); - } - - if (getIteratorFn(newChild)) { - return reconcileChildrenIterator( - returnFiber, - currentFirstChild, - newChild, - lanes, - ); - } - - if (isObject) { - throwOnInvalidObjectType(returnFiber, newChild); - } - if (__DEV__) { if (typeof newChild === 'function') { warnOnFunctionType(returnFiber); diff --git a/packages/react-reconciler/src/ReactChildFiber.old.js b/packages/react-reconciler/src/ReactChildFiber.old.js index 34120a641ba3b..80b4fc35512d0 100644 --- a/packages/react-reconciler/src/ReactChildFiber.old.js +++ b/packages/react-reconciler/src/ReactChildFiber.old.js @@ -1231,9 +1231,7 @@ function ChildReconciler(shouldTrackSideEffects) { } // Handle object types - const isObject = typeof newChild === 'object' && newChild !== null; - - if (isObject) { + if (typeof newChild === 'object' && newChild !== null) { switch (newChild.$$typeof) { case REACT_ELEMENT_TYPE: return placeSingleChild( @@ -1266,6 +1264,26 @@ function ChildReconciler(shouldTrackSideEffects) { ); } } + + if (isArray(newChild)) { + return reconcileChildrenArray( + returnFiber, + currentFirstChild, + newChild, + lanes, + ); + } + + if (getIteratorFn(newChild)) { + return reconcileChildrenIterator( + returnFiber, + currentFirstChild, + newChild, + lanes, + ); + } + + throwOnInvalidObjectType(returnFiber, newChild); } if (typeof newChild === 'string' || typeof newChild === 'number') { @@ -1279,28 +1297,6 @@ function ChildReconciler(shouldTrackSideEffects) { ); } - if (isArray(newChild)) { - return reconcileChildrenArray( - returnFiber, - currentFirstChild, - newChild, - lanes, - ); - } - - if (getIteratorFn(newChild)) { - return reconcileChildrenIterator( - returnFiber, - currentFirstChild, - newChild, - lanes, - ); - } - - if (isObject) { - throwOnInvalidObjectType(returnFiber, newChild); - } - if (__DEV__) { if (typeof newChild === 'function') { warnOnFunctionType(returnFiber);