Skip to content

Instantly share code, notes, and snippets.

@juriadams
Created July 17, 2023 10:51
Show Gist options
  • Save juriadams/677f84241378089111628b11f35fcfee to your computer and use it in GitHub Desktop.
Save juriadams/677f84241378089111628b11f35fcfee to your computer and use it in GitHub Desktop.
Render React Component to HTML String with Type-Safe Prop Validation
import { renderToStaticMarkup } from 'react-dom/server';
import { type ComponentType } from 'react';
/**
* Converts a specified React component to its HTML string representation.
* @param Component A React component to be rendered, for instance, `AccountSignInEmail`.
* @param props An object containing the props to be passed to the Component. Type safety is enforced.
* @returns The HTML string representation of the rendered component.
*/
export const render = <P extends object>(Component: ComponentType<P>, props: P): string =>
renderToStaticMarkup(<Component {...props} />);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment