Skip to content

Commit 95252b0

Browse files
committed
Handle different InPortal/OutPortal rendering order
1 parent b2c8993 commit 95252b0

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/index.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ export const createPortalNode = <C extends Component<any>>(): PortalNode<C> => {
9393
return portalNode;
9494
};
9595

96-
export class InPortal extends React.PureComponent<InPortalProps, { nodeProps: {} }> {
96+
export class InPortal extends React.PureComponent<InPortalProps, { nodeProps: {}, isFirstRender: boolean }> {
9797

9898
constructor(props: InPortalProps) {
9999
super(props);
100100
this.state = {
101-
nodeProps: this.props.node.getInitialPortalProps()
101+
nodeProps: this.props.node.getInitialPortalProps(),
102+
isFirstRender: true,
102103
};
103104
}
104105

@@ -122,17 +123,20 @@ export class InPortal extends React.PureComponent<InPortalProps, { nodeProps: {}
122123
render() {
123124
const { children, node } = this.props;
124125

125-
if (node.element) {
126-
return ReactDOM.createPortal(
127-
React.Children.map(children, (child) => {
128-
if (!React.isValidElement(child)) return child;
129-
return React.cloneElement(child, this.state.nodeProps)
130-
}),
131-
node.element
132-
);
126+
if (!node.element && this.state.isFirstRender) {
127+
// If the InPortal is rendered before the OutPortal then the portal element won't exist yet:
128+
// wait for it to initialize and then try again
129+
setTimeout(() => this.setState({ isFirstRender: false }))
130+
return null;
133131
}
134-
// Else: the portalNode has not been mounted yet
135-
return null;
132+
133+
return ReactDOM.createPortal(
134+
React.Children.map(children, (child) => {
135+
if (!React.isValidElement(child)) return child;
136+
return React.cloneElement(child, this.state.nodeProps)
137+
}),
138+
node.element as PortalNodeElement
139+
);
136140
}
137141
}
138142

0 commit comments

Comments
 (0)