Last active
August 18, 2023 04:13
-
-
Save jericbas/c17890bef15515c0c9b3f49bb918a67e to your computer and use it in GitHub Desktop.
ObjectToJSX
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 React, { ReactNode } from 'react'; | |
interface ObjectProps { | |
[key: string]: any; | |
children?: ReactNode; | |
} | |
function objectToJSX(component: string, object: ObjectProps): JSX.Element { | |
if (typeof component !== 'string' || typeof object !== 'object') { | |
throw new Error('Invalid arguments provided.'); | |
} | |
const props = { ...object }; | |
const children = props.children; | |
delete props.children; // Remove children from props | |
if (children && typeof children !== 'string' && !React.isValidElement(children)) { | |
throw new Error('Invalid children prop provided.'); | |
} | |
return React.createElement(component, props, children); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment