1

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.

1 Answer 1

2

The component is just a function and it's props are just the first parameter to that function. One can derive the parameter type from a function type:

 function GenericComponent<T extends Function>(
   props: { tag: T } & { [key in keyof Parameters<T>[0]]: Parameters<T>[0][key] }
 ) { /*...*/ }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.