import React from "react" ; import ReactDOM from "react-dom" ; export interface PortalDOM { element : HTMLDivElement ; mount : ( component : React . ReactNode ) => Promise < void > ; destroy : ( ) => void ; } param point returns export function createPortalDOM ( point ? ) { const element = document . createElement ( "div" ) ; let mountPoint = document . body ; if ( point instanceof HTMLElement ) { mountPoint = point ; } mountPoint . appendChild ( element ) ; return { element , async mount ( component ) { return new Promise < void > ( ( resolve ) => { ReactDOM . render ( < > { component } < />, element, resolve); } ) ; } , destroy ( ) { if ( element instanceof HTMLDivElement ) { ReactDOM . unmountComponentAtNode ( element ) ; if ( typeof element . remove === "function" ) { element . remove ( ) ; } else { mountPoint . removeChild ( element ) ; } } } , } ; }