Skip to content

Instantly share code, notes, and snippets.

@jericbas
Last active August 18, 2023 04:13
Show Gist options
  • Save jericbas/c17890bef15515c0c9b3f49bb918a67e to your computer and use it in GitHub Desktop.
Save jericbas/c17890bef15515c0c9b3f49bb918a67e to your computer and use it in GitHub Desktop.
ObjectToJSX
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