@@ -9,7 +9,7 @@ import { warnOnce } from '../logging';
99export 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
2525function 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