Skip to content

Commit fde6602

Browse files
gaearonacusti
authored andcommitted
Remove recursion from unmounting portals (facebook#1)
1 parent 351bf56 commit fde6602

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/renderers/shared/fiber/ReactFiberCommitWork.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,11 @@ module.exports = function<T, P, I, TI, C>(
207207
// node from the tree.
208208
removeChild(parent, node.stateNode);
209209
} else if (node.tag === Portal) {
210-
// If this is a portal, then the parent is actually the portal itself.
211-
// We need to keep track of which parent we're removing from.
212-
// TODO: This uses a recursive call. We can get rid of that by mutating
213-
// the parent binding and restoring it by searching for the host parent
214-
// again when we pop past a portal.
215-
const portalParent = node.stateNode.containerInfo;
216-
let child = node.child;
217-
while (child) {
218-
unmountHostComponents(portalParent, child);
219-
child = child.sibling;
220-
}
210+
// When we go into a portal, it becomes the parent to remove from.
211+
// We will reassign it back when we pop the portal on the way up.
212+
parent = node.stateNode.containerInfo;
213+
node = node.child;
214+
continue;
221215
} else {
222216
commitUnmount(node);
223217
if (node.child) {
@@ -235,6 +229,11 @@ module.exports = function<T, P, I, TI, C>(
235229
return;
236230
}
237231
node = node.return;
232+
if (node.tag === Portal) {
233+
// When we go out of the portal, we need to restore the parent.
234+
// Since we don't keep a stack of them, we will search for it.
235+
parent = getHostParent(node);
236+
}
238237
}
239238
node.sibling.return = node.return;
240239
node = node.sibling;

0 commit comments

Comments
 (0)