开发者

NextJS TypeScript How to define Enum for strings

开发者 https://www.devze.com 2022-12-07 18:25 出处:网络
In my nextjs-app I have a component, which has a prop of the type string. Now I want to define an enum, so I tried to do this:

In my nextjs-app I have a component, which has a prop of the type string. Now I want to define an enum, so I tried to do this:

enum Label {
 dermatology = 'Dermatologi',
 psychology = 'Psykologi',
 rheumatology = 'Reumatologi',
 physiology = 'Fysiologi',

}

interface ISpecialist {
 开发者_开发问答 label?: Label
}

export default function Specialist({ specialist }: { specialist: ISpecialist }) {
   return (
      <div>
        <span>{specialist.label === Label}
      </div>
   )
}

but this doesn't work - can someone help me out?

the prop label is as mentioned before of the type string and the values are for example 'psychology' or 'dermatology'


If you want to print the corresponding value of the enum, access it like the enum key.

    <span>{Label[specialist.label] }


In your code, you are trying to compare the label prop to the Label enum itself, which is not valid. To compare the label prop to a specific value in the Label enum, you need to use the dot notation to refer to the value you want to compare against. For example, you can use the following code to compare the label prop to the Dermatology value in the Label enum:

<span>{specialist.label === Label.dermatology}</span>
0

精彩评论

暂无评论...
验证码 换一张
取 消