Merge isObject branches (#21226)
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.
Sebastian Markbåge committed
Apr 9, 2021 at 19:19 UTC
39713716aa09c025bb4c3c8c0c9d7979946250c7
2 files changed
+42
-50
packages/react-reconciler/src/ReactChildFiber.new.js
+21
-25
@@ -1231,9 +1231,7 @@ function ChildReconciler(shouldTrackSideEffects) {
1231
}
1232
1233
// Handle object types
1234
- const isObject = typeof newChild === 'object' && newChild !== null;
1235
-
1236
- if (isObject) {
1234
+ if (typeof newChild === 'object' && newChild !== null) {
1235
switch (newChild.$$typeof) {
1236
case REACT_ELEMENT_TYPE:
1237
return placeSingleChild(
@@ -1266,6 +1264,26 @@ function ChildReconciler(shouldTrackSideEffects) {
1264
);
1265
}
1266
}
1267
+
1268
+ if (isArray(newChild)) {
1269
+ return reconcileChildrenArray(
1270
+ returnFiber,
1271
+ currentFirstChild,
1272
+ newChild,
1273
+ lanes,
1274
+ );
1275
+ }
1276
+
1277
+ if (getIteratorFn(newChild)) {
1278
+ return reconcileChildrenIterator(
1279
+ returnFiber,
1280
+ currentFirstChild,
1281
+ newChild,
1282
+ lanes,
1283
+ );
1284
+ }
1285
+
1286
+ throwOnInvalidObjectType(returnFiber, newChild);
1287
}
1288
1289
if (typeof newChild === 'string' || typeof newChild === 'number') {
@@ -1279,28 +1297,6 @@ function ChildReconciler(shouldTrackSideEffects) {
1297
);
1298
}
1299
1282
- if (isArray(newChild)) {
1283
- return reconcileChildrenArray(
1284
- returnFiber,
1285
- currentFirstChild,
1286
- newChild,
1287
- lanes,
1288
- );
1289
- }
1290
-
1291
- if (getIteratorFn(newChild)) {
1292
- return reconcileChildrenIterator(
1293
- returnFiber,
1294
- currentFirstChild,
1295
- newChild,
1296
- lanes,
1297
- );
1298
- }
1299
-
1300
- if (isObject) {
1301
- throwOnInvalidObjectType(returnFiber, newChild);
1302
- }
1303
-
1300
if (__DEV__) {
1301
if (typeof newChild === 'function') {
1302
warnOnFunctionType(returnFiber);
packages/react-reconciler/src/ReactChildFiber.old.js
+21
-25
@@ -1231,9 +1231,7 @@ function ChildReconciler(shouldTrackSideEffects) {
1231
}
1232
1233
// Handle object types
1234
- const isObject = typeof newChild === 'object' && newChild !== null;
1235
-
1236
- if (isObject) {
1234
+ if (typeof newChild === 'object' && newChild !== null) {
1235
switch (newChild.$$typeof) {
1236
case REACT_ELEMENT_TYPE:
1237
return placeSingleChild(
@@ -1266,6 +1264,26 @@ function ChildReconciler(shouldTrackSideEffects) {
1264
);
1265
}
1266
}
1267
+
1268
+ if (isArray(newChild)) {
1269
+ return reconcileChildrenArray(
1270
+ returnFiber,
1271
+ currentFirstChild,
1272
+ newChild,
1273
+ lanes,
1274
+ );
1275
+ }
1276
+
1277
+ if (getIteratorFn(newChild)) {
1278
+ return reconcileChildrenIterator(
1279
+ returnFiber,
1280
+ currentFirstChild,
1281
+ newChild,
1282
+ lanes,
1283
+ );
1284
+ }
1285
+
1286
+ throwOnInvalidObjectType(returnFiber, newChild);
1287
}
1288
1289
if (typeof newChild === 'string' || typeof newChild === 'number') {
@@ -1279,28 +1297,6 @@ function ChildReconciler(shouldTrackSideEffects) {
1297
);
1298
}
1299
1282
- if (isArray(newChild)) {
1283
- return reconcileChildrenArray(
1284
- returnFiber,
1285
- currentFirstChild,
1286
- newChild,
1287
- lanes,
1288
- );
1289
- }
1290
-
1291
- if (getIteratorFn(newChild)) {
1292
- return reconcileChildrenIterator(
1293
- returnFiber,
1294
- currentFirstChild,
1295
- newChild,
1296
- lanes,
1297
- );
1298
- }
1299
-
1300
- if (isObject) {
1301
- throwOnInvalidObjectType(returnFiber, newChild);
1302
- }
1303
-
1300
if (__DEV__) {
1301
if (typeof newChild === 'function') {
1302
warnOnFunctionType(returnFiber);