Skip to content

Instantly share code, notes, and snippets.

@4msar
Created February 19, 2024 09:39
Show Gist options
  • Save 4msar/bd921473bcadfef778705c84b146bb31 to your computer and use it in GitHub Desktop.
Save 4msar/bd921473bcadfef778705c84b146bb31 to your computer and use it in GitHub Desktop.
Render React Component to Iframe
import React, { useRef } from "react"
import { createPortal } from "react-dom"
export const IFrame = ({
children,
title,
...props
}: HTMLAttributes<HTMLIFrameElement> & {
children: React.ReactNode
}) => {
const contentRef = useRef<HTMLIFrameElement>(null)
const mountNode = contentRef?.current?.contentWindow?.document?.body
return (
<iframe title={title} {...props} ref={contentRef}>
{mountNode && createPortal(children, mountNode)}
</iframe>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment