Skip to content

Instantly share code, notes, and snippets.

@keichan34
Created September 11, 2020 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keichan34/d2beb28ec438632b8fdd166b39034821 to your computer and use it in GitHub Desktop.
Save keichan34/d2beb28ec438632b8fdd166b39034821 to your computer and use it in GitHub Desktop.
Open a new window with a React portal
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