Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Property does not exist' in a Functional Component with added functional components as properties to it?

I use React with Typescript and a functional approach.

const Divider: React.FunctionComponent<CardDividerProps> = (props: CardDividerProps) => (
   <div>
      divider
   </div>
);

const Card: React.FunctionComponent<CardProps> = (props: CardProps) => (
   <div>
      card
   </div>
);

Card.Divider = Divider; //Property 'Divider' does not exist on type 'FunctionComponent<CardProps>'.

If I remove the explicit type from the Card, it works. But I want to specify it with React.FunctionComponent... Is it possible?

I guess I can create a type:

type CardWithDividerComponent = {
    (props: CardProps): JSX.Element;
    defaultProps: CardProps;
    Divider: React.FunctionComponent<CardDividerProps>;
}

But is this the only solution? Is there any solution with React.FunctionComponent?

like image 358
Nikolai Avatar asked Oct 25 '25 04:10

Nikolai


1 Answers

You are telling TypeScript that Card is a function with type React.FC. This type doesn't contain any property Divider, so TypeScript complains about that.

To fix the issue, you must tell TypeScript correct type of your component, something like this:

const Card: React.FunctionComponent<CardProps> & {Divider?: React.FunctionComponent<CardDividerProps>} = (props: CardProps) => (
   <div>
      card
   </div>
);

Card.Divider = Divider;
like image 103
Drag13 Avatar answered Oct 26 '25 18:10

Drag13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!