如何使用enum作为react / typescript中的道具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用enum作为react / typescript中的道具相关的知识,希望对你有一定的参考价值。

我有以下枚举

export enum Sizes{
    Small,
    Large
}

这是在我的<Demo/>组件的道具界面中使用的:

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

我的问题是,当我使用这个<Demo Size={how do i define size here?} />

答案

您可以像在任何其他上下文中一样引用枚举值:

export enum Sizes{
    Small,
    Large
}

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

class Demo extends React.Component<IProps> {}

let d = <Demo Size={Sizes.Large} />

以上是关于如何使用enum作为react / typescript中的道具的主要内容,如果未能解决你的问题,请参考以下文章