Server Rendering to other strings
Unanswered
American Shorthair posted this in #help-forum
American ShorthairOP
Playing around with rendering a React component tree as LaTeX. Would be nice if I could, in an API call, pass this tree to
???
I basically want to call some server-side functions that transform the tree to LaTeX and from there to PDF.
renderToString
or renderToReadableStream
but Next really doesn't want to support that. Is there an escape hatch from ./app/api/route.tsx
ReactServerComponentsError:
You're importing a component that imports react-dom/server. To fix it, render or return the content directly as a Server Component instead for perf and security.
Learn more: https://nextjs.org/docs/getting-started/react-essentials
Maybe one of these should be marked as a client entry "use client":
./app/api/route.tsx
???
I basically want to call some server-side functions that transform the tree to LaTeX and from there to PDF.
1 Reply
American ShorthairOP
This appears to work:
const MyComponent = () => {
return (
<div>Hello, world!</div>
);
};
export async function GET(req: any, res: any) {
const ReactDOMServer = (await import('react-dom/server')).default;
const component = <MyComponent />;
const result = ReactDOMServer.renderToString(component);
return result;
}
const MyComponent = () => {
return (
<div>Hello, world!</div>
);
};
export async function GET(req: any, res: any) {
const ReactDOMServer = (await import('react-dom/server')).default;
const component = <MyComponent />;
const result = ReactDOMServer.renderToString(component);
return result;
}