diff --git a/desktop/app/src/utils/renderReactRoot.tsx b/desktop/app/src/utils/renderReactRoot.tsx new file mode 100644 index 000000000..09699d2d1 --- /dev/null +++ b/desktop/app/src/utils/renderReactRoot.tsx @@ -0,0 +1,27 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import {render, unmountComponentAtNode} from 'react-dom'; + +/** + * This utility creates a fresh react render hook, which is great to render elements imperatively, like opening dialogs. + * Make sure to call `unmount` to cleanup after the rendering becomes irrelevant + */ +export function renderReactRoot( + handler: (unmount: () => void) => React.ReactElement, +): void { + const div = document.body.appendChild(document.createElement('div')); + render( + handler(() => { + unmountComponentAtNode(div); + div.remove(); + }), + div, + ); +}