@@ -207,17 +207,11 @@ module.exports = function<T, P, I, TI, C>(
207
207
// node from the tree.
208
208
removeChild ( parent , node . stateNode ) ;
209
209
} 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 ;
221
215
} else {
222
216
commitUnmount ( node ) ;
223
217
if ( node . child ) {
@@ -235,6 +229,11 @@ module.exports = function<T, P, I, TI, C>(
235
229
return ;
236
230
}
237
231
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
+ }
238
237
}
239
238
node . sibling . return = node . return ;
240
239
node = node . sibling ;
0 commit comments