renderToString
Reference
renderToString(element: JSX.Element, request?: Request): Promise<string>
The renderToString function is used to render a JSX element to a string on the server side.
Parameters:
element: The JSX element to render.options(optional): An optional object withrequestandapplySuspense.
Types:
export async function renderToString(
element: JSX.Element,
options: { request?: Request; applySuspense?: boolean } = {},
): Promise<string>;
The
requestparameter by default isnew Request('http://localhost'). The request object can be used inside the JSX Server Components, for this, you can change the default value to the request object that you want to use.
The default value for
applySuspenseisfalse. Normally, we only need suspense during HTML streaming, so we don't need to apply it when rendering to a string. However, if you want to apply suspense, you can setapplySuspensetotrue.
Returns:
- A
Promisethat resolves to a string representing the rendered JSX element.
Example usage:
import { renderToString } from "brisa";
const htmlString = await renderToString(<div>Hello, world!</div>);
console.log(htmlString); // Output: '<div>Hello, world!</div>'
In principle you should never use it inside Brisa Framework.