Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To make JSX.Element properties of Typescript compatible with ComponentType type, you can do the following:

  1. Import the ComponentType type from the React library:
import { ComponentType } from 'react';
  1. Define a type for your custom component props:
type CustomProps = {
  myProp: string;
};
  1. Define your custom component with the CustomProps type:
function CustomComponent(props: CustomProps) {
  ...
}
  1. Define a variable of type ComponentType and assign it your custom component:
const MyComponent: ComponentType<CustomProps> = CustomComponent;
  1. Pass the MyComponent to another component as a prop:
function ParentComponent() {
  return <ChildComponent myComponent={MyComponent} />;
}
  1. Retrieve the component from the prop and render it:
function ChildComponent(props: { myComponent: ComponentType<CustomProps> }) {
  const MyComponent = props.myComponent;
  return <MyComponent myProp="Hello world" />;
}