Created
July 17, 2023 10:51
-
-
Save juriadams/677f84241378089111628b11f35fcfee to your computer and use it in GitHub Desktop.
Render React Component to HTML String with Type-Safe Prop Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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