Created
September 11, 2020 02:03
-
-
Save keichan34/d2beb28ec438632b8fdd166b39034821 to your computer and use it in GitHub Desktop.
Open a new window with a React portal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useMemo, useRef } from "react" | |
import { createPortal } from "react-dom" | |
const NewWindowPortal: React.FC = ({children}) => { | |
const containerEl = useMemo(() => document.createElement("div"), []) | |
useEffect(() => { | |
if (!containerEl) { | |
return | |
} | |
const newWindow = window.open('', '', 'width=200,height=800') | |
newWindow.document.body.appendChild(containerEl) | |
return () => { | |
newWindow.close() | |
} | |
}, [containerEl]) | |
return createPortal(children, containerEl) | |
} | |
export default NewWindowPortal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment