Search code examples
reactjstypescriptenums

How to use enum as props in React/TypeScript


I have the following enum

export enum Sizes{
    Small,
    Large
}

which is getting used in my <Demo/> component's props interface:

export interface IProps{
    Label?: string;
    Size: SizeEnum;
}

My question is, when I use this <Demo Size={how do i define size here?} />?


Solution

  • You can just reference the enum value as you would in any other context:

    export enum Sizes{
        Small,
        Large
    }
    
    export interface IProps{
        Label?: string;
        Size: Sizes;
    }
    
    class Demo extends React.Component<IProps> {}
    
    let d = <Demo Size={Sizes.Large} />