如何使用多个选择器重用相同的样式规则

时间:2017-08-07 22:20:18

标签: javascript css reactjs styles jss

我希望使用jss样式。

.a{
height: 20px;
}
.b{
height: 40px;
}
.a,.b{
width: 100px;
}

尝试1

制定规则c并将该课程添加到ab

c: {
width: '100px'
}

尝试2

制作一个对象common并将其合并到ab规则

const common = {
   width: '100px',
};

a: {
height: '20px',
...common
}
b: {
height: '40px',
...common
}

有没有更好的方法?

2 个答案:

答案 0 :(得分:2)

我觉得更容易解析的一个更简单的替代方法是,通过用引号引起来将键设置为字符串:

'.a, .b': {
   width: 100px; 
}

答案 1 :(得分:1)

扩展插件(默认启用)如何?

https://cssinjs.org/jss-plugin-extend

const styles = {
  buttonColor: {
    background: 'red'
  },
  button: {
    extend: 'buttonColor',
    fontSize: '20px'
  }
}