简体   繁体   English

如何在反应中创建动态 css class

[英]how to create dynamic css class in react

i want to add following class in class name dynamicaly so i wont have to make diffrent component for all the classes can any one help me solve this problem it would be big help if you can thank you我想在 class 名称中动态添加以下 class 所以我不必为所有类制作不同的组件 任何人都可以帮我解决这个问题,如果你能谢谢你,那将是很大的帮助

 <AnnotationLabel
                x={120}
                y={160}
                ny={140}
                nx={20}
                color={"#afb1b3"}
                editMode={false}
                className="country-bg-usa hand-cursor"
                note={{
                    "title": "USA".toUpperCase(),
                    "align": "middle",
                    "orientation": "topBottom",
                    "bgPadding": { left: 7, right: 5 },
                    "padding": 0,
                    "titleColor": "white"
                }}
                events={{
                    onClick: () => gotoCountry("USA")
                }}
            />

here is the css i want to place in className properties这是我想放在 className 属性中的 css

   .country-bg-canada .annotation-note-bg {
    height: 23px;
      fill: #ff0000;
     fill-opacity: 1;
    }
  .country-bg-usa .annotation-note-bg {
    height: 23px;
   fill: #1d1de0;
   fill-opacity: 1;
  }
 .country-bg-suriname  .annotation-note-bg {
  height: 23px;
  width: 56px;
   fill: #f8b13e;
   fill-opacity: 1;
 }

      .country-bg-ireland .annotation-note-bg {
    height: 23px;
     width: 45px;
      fill: #008000;
           fill-opacity: 1;
         }
         .country-bg-unitedKingdom .annotation-note-bg {
      height: 23px;
        width: 88px;
       fill: #0B52F7;
       fill-opacity: 1;
       }
         .country-bg-singapore .annotation-note-bg {
        height: 23px;
          width: 60px;
   fill: #D42441;
      fill-opacity: 1;
         }

You can do something like this:你可以这样做:

 <AnnotationLabel
                x={120}
                y={160}
                ny={140}
                nx={20}
                color={"#afb1b3"}
                editMode={false}
                className={`country-bg-${Your-country} hand-cursor`} // use the template string
                note={{
                    "title": "USA".toUpperCase(),
                    "align": "middle",
                    "orientation": "topBottom",
                    "bgPadding": { left: 7, right: 5 },
                    "padding": 0,
                    "titleColor": "white"
                }}
                events={{
                    onClick: () => gotoCountry("USA")
                }}
            />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM