Not sure if the name "generic component" is correct, nonetheless I cannot come up with the right way of doing that.
The idea is to have a component that accepts tag
as a prop, and that tag
can be either a string (tag name) or a component constructor (FC, class component).
Here's the example code of what I'm trying to achieve
export const MyComponent = () => {
return (
<GeneicComponent tag="span">
{/* some content here */}
</GeneicComponent>
)
}
export const MyComponent2 = () => {
return (
<GeneicComponent tag={SomeCustomComponent} {/* SomeCustomComponents props */}>
{/* some content here */}
</GeneicComponent>
)
}
This approach works just fine until you're trying to pass extra props. GenericComponent must infer those props from tag
and extend its own props with inferred ones, yet I have no idea now how to do that. Another problem is to infer props when the tag name passed as a string. I'm pretty sure it's something related to JSX.IntrinsicElements
but had no luck experimenting with that.
Appreciate any suggestions.