css - 如何在 react js 代码中动态更改 css 选择器属性值?

标签 css reactjs sass css-selectors radium

我需要为特定选择器动态更改 react 组件中的颜色。 在 scss(使用 sass)中我有以下规则:

foo.bar.var * {
  color: blue;
}

我想在 React 代码中将其更改为黄色、红色或其他颜色。

I cant use style property for element, cause i need the selector to apply for all subchilds !=)

有什么原生方法吗?或者我应该使用镭?或者有没有类似的库?也许 css-next some hove 可以帮助解决这个问题?

我有颜色选择器,我不能为每种颜色写类样式 =(

对于一些回答者注意:

So i have selector in some scss file, that imported in some root js file with .class * {color: $somecolor} and i need change the $somecolor in that selector, during picking colors in color picker

也许我可以以某种方式为所有嵌套在 style 属性中的选择器设置?还是有办法为 style prop 中的每个嵌套元素递归应用 css 样式?

最佳答案

关于

class MyComponent extends React.Component {
  render() {
    const yellow = true // Your condition
    
    return(
      <div className={`foo bar var ${yellow  && 'yellow'}`}
        My item
      </div>
    )
  }
}
.foo.bar.var {
  & * {
    color: blue;
  }
  &.yellow * {
    color: yellow;
  }
}

关于css - 如何在 react js 代码中动态更改 css 选择器属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46684012/

相关文章:

reactjs - 无法从适用于 Chrome 的 React DevTools 修改组件属性

reactjs - 如何将 scss 文件导入到 typescript 文件中

javascript - Webpack 4 中 css-loader 的 importLoaders 选项到底是什么?

html - 截面高度 100% 不起作用

css - 表格标题背景图像垂直平铺

javascript - 使用不同的图标重新渲染标题 - React Native

css - 使用 css 在 ionic 输入上书写时更改输入文本的颜色

css - 为什么页脚没有粘在底部?

css - Chrome 37 不支持 Calibri Light

database - 如何使 React 状态与 MySQL 数据库保持同步