@samitouri / QOS-React / commits / 7fbdfd9f5a

Copied findCurrentFiberUsingSlowPath Suspense bug fix from recent PR https://github.com/facebook/react/pull/15312/files

Brian Vaughn committed Apr 3, 2019 at 18:20 UTC 7fbdfd9f5ac5696a7a6b14267ddb1d9f729b603f
1 file changed +19 -4
src/backend/renderer.js
+19 -4
@@ -1105,6 +1105,7 @@ export function attach(
1105 // This function is copied from React and should be kept in sync:
1106 // https://github.com/facebook/react/blob/master/packages/react-reconciler/src/ReactFiberTreeReflection.js
1107 // It would be nice if we updated React to inject this function directly (vs just indirectly via findDOMNode).
1108 + // BEGIN copied code
1109 function findCurrentFiberUsingSlowPath(fiber: Fiber): Fiber | null {
1110 let alternate = fiber.alternate;
1111 if (!alternate) {
@@ -1121,15 +1122,28 @@ export function attach(
1122 // If we have two possible branches, we'll walk backwards up to the root
1123 // to see what path the root points to. On the way we may hit one of the
1124 // special cases and we'll deal with them.
1124 - let a = fiber;
1125 - let b = alternate;
1125 + let a: Fiber = fiber;
1126 + let b: Fiber = alternate;
1127 while (true) {
1128 let parentA = a.return;
1128 - let parentB = parentA ? parentA.alternate : null;
1129 - if (!parentA || !parentB) {
1129 + if (parentA === null) {
1130 // We're at the root.
1131 break;
1132 }
1133 + let parentB = parentA.alternate;
1134 + if (parentB === null) {
1135 + // There is no alternate. This is an unusual case. Currently, it only
1136 + // happens when a Suspense component is hidden. An extra fragment fiber
1137 + // is inserted in between the Suspense fiber and its children. Skip
1138 + // over this extra fragment fiber and proceed to the next parent.
1139 + const nextParent = parentA.return;
1140 + if (nextParent !== null) {
1141 + a = b = nextParent;
1142 + continue;
1143 + }
1144 + // If there's no parent, we're at the root.
1145 + break;
1146 + }
1147
1148 // If both copies of the parent fiber point to the same child, we can
1149 // assume that the child is current. This happens when we bailout on low
@@ -1234,6 +1248,7 @@ export function attach(
1248 // Otherwise B has to be current branch.
1249 return alternate;
1250 }
1251 + // END copied code
1252
1253 function selectElement(id: number): void {
1254 let fiber = idToFiberMap.get(id);