Why can't I use renderToString
anymore? How can I print now?
#57631
-
I was using I was using: import { renderToString } from 'react-dom/server'
const html = renderToString( <SomeReactNode /> )
const iframe = document.createElement( 'iframe' )
iframe.id = 'print_pdf'
iframe.style.width = '100vw'
iframe.style.height = '100vh'
document.body.appendChild( iframe )
iframe.contentWindow?.document.open()
iframe.contentWindow?.document.write( html )
iframe.contentWindow?.document.close()
iframe.contentWindow?.print()
document.body.removeChild( iframe ) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
I don't know the answer but the error says to open an issue in this repo, have you done that ? |
Beta Was this translation helpful? Give feedback.
-
As suggested here
|
Beta Was this translation helpful? Give feedback.
-
I think if you want to render into an iframe with React, you can do: "use client";
import { PropsWithChildren, useState } from "react";
import { createPortal } from "react-dom";
const CustomIframe = ({ children, ...props }: PropsWithChildren) => {
const [contentRef, setContentRef] = useState<HTMLIFrameElement | null>(null);
const mountNode = contentRef?.contentWindow?.document?.body;
return (
<iframe {...props} ref={setContentRef}>
{mountNode && createPortal(children, mountNode)}
</iframe>
);
};
export default CustomIframe; And then: import CustomIframe from "./CustomIframe";
export default function Home() {
return (
<CustomIframe>
<h1>Hello World!</h1>
</CustomIframe>
);
} If you don't want to do the Portal trick, you might want to try and dynamically import export default async function Home() {
const { renderToString } = await import("react-dom/server");
return (
<div
dangerouslySetInnerHTML={{
__html: `${renderToString(<h1>Hello world 2</h1>)}`,
}}
/>
);
} |
Beta Was this translation helpful? Give feedback.
Yes, because I'm trying to do it with a button onClick.
Thanks for all your help. I was able to find this video, and it worked for me.