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

问题描述 投票:2回答:1

我有以下枚举

export enum Sizes{
    Small,
    Large
}

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

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

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

reactjs typescript enums
1个回答
4
投票

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

export enum Sizes{
    Small,
    Large
}

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

class Demo extends React.Component<IProps> {}

let d = <Demo Size={Sizes.Large} />
© www.soinside.com 2019 - 2024. All rights reserved.