Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How pass React prop to CSS

I have a problem - I don't know how to change checkbox color using a color value from props. My idea was to give it via style attribute but I don't know how to toggle this. I'm using rc-switch and I want to change his background depending on Switch state. I have something like this now

<Switch style={{ backgroundColor: mainColor }}/>

but it set this color for both states and I want this swich to become 'defaultColor' when is in off position.

like image 382
Alan Wołejko Avatar asked Jan 30 '23 02:01

Alan Wołejko


1 Answers

There is no style prop on the Switch Component, but there is a className prop, which you can use to add your custom class.

If you are using sass:

.mySwitch {
  &-black {
    background-color: black;
  }

  &-yellow {
    background-color: yellow;
  }
}

Then programatically switch the class.

<Switch className={`mySwitch-${color}` ... />

Could be an option, I think.

like image 179
webdeb Avatar answered Feb 02 '23 17:02

webdeb