Skip to content

Commit 5c3f2c9

Browse files
committed
fix: Proper async handling for portals
1 parent 54c5ad9 commit 5c3f2c9

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/internal/portal/index.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { warnOnce } from '../logging';
99
export interface PortalProps {
1010
container?: null | Element;
1111
getContainer?: () => Promise<HTMLElement>;
12-
removeContainer?: (container: HTMLElement) => void;
12+
removeContainer?: (container: HTMLElement | null) => void;
1313
children: React.ReactNode;
1414
}
1515

@@ -23,13 +23,17 @@ function manageDefaultContainer(setState: React.Dispatch<React.SetStateAction<El
2323
}
2424

2525
function manageAsyncContainer(
26-
getContainer: () => Promise<HTMLElement>,
27-
removeContainer: (container: HTMLElement) => void,
26+
getContainer: (options: { abortSignal: AbortSignal }) => Promise<HTMLElement>,
27+
removeContainer: (container: HTMLElement | null) => void,
2828
setState: React.Dispatch<React.SetStateAction<Element | null>>
2929
) {
30-
let newContainer: HTMLElement;
31-
getContainer().then(
30+
let newContainer: HTMLElement | null = null;
31+
const abortController = new AbortController();
32+
getContainer({ abortSignal: abortController.signal }).then(
3233
container => {
34+
if (abortController.signal.aborted) {
35+
return;
36+
}
3337
newContainer = container;
3438
setState(container);
3539
},
@@ -38,6 +42,7 @@ function manageAsyncContainer(
3842
}
3943
);
4044
return () => {
45+
abortController.abort();
4146
removeContainer(newContainer);
4247
};
4348
}

0 commit comments

Comments
 (0)