reactjs 如何在反应/打字稿中使用枚举作为道具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49746638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:31:23  来源:igfitidea点击:

How to use enum as props in react/typescript

reactjstypescriptenums

提问by ZeroDarkThirty

I have the following enum

我有以下枚举

export enum Sizes{
    Small,
    Large
}

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

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

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

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

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

回答by Titian Cernicova-Dragomir

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} />