Skip to content

Commit fdbae73

Browse files
committed
Avoid crashing renders given rendering races
If two OutPortals are ever rendered at the same time, the second will steal the content from the first. When the first unmounts, it accidentally unmounts the content from the second, so when the second unmounts, it crashes. This will always happen if you have two renders simultaneously, and it seems it can also happen if they're not simultaneous but maybe React has only started unmounting one element when it mounts another (is this possible? seems to be happening, but very unclear). I'll extend this to avoid the issue entirely shortly, but this fixes the immediate crash.
1 parent 8805c73 commit fdbae73

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/index.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ export class OutPortal<C extends Component<any>> extends React.PureComponent<Out
101101
}
102102

103103
componentWillUnmount() {
104-
const { node } = this.props;
105-
node.parentNode.replaceChild(
106-
this.placeholderNode.current,
107-
node
108-
);
109-
this.props.node.setPortalProps({});
104+
const node = this.props.node as PortalNode<C>;
105+
106+
if (node.parentNode) {
107+
node.parentNode.replaceChild(
108+
this.placeholderNode.current!,
109+
node
110+
);
111+
this.props.node.setPortalProps({});
112+
}
110113
}
111114

112115
render() {

0 commit comments

Comments
 (0)