在使用情感js编写CSS时,当您在嵌套样式上使用cx
时,样式嵌套将被完全破坏。但是,如果我正确理解该库,那么您应该这样做。下面说明了这个问题:
import { cx,css } from 'emotion';
const child = css`
background-color: blue;
`;
const parent = css`
background-color: green;
&.${child} {
background-color: red;
}
`;
const other = css`
/* I do nothing */
`;
export default class MyComponent extends React.PureComponent {
render = () => (
<div>
<div classname={ cx(child) }>
My background is blue.
</div>
<div classname={ cx(parent) }>
<div classname={ cx(child) }>
My background is red.
</div>
</div>
<div classname={ cx(parent) }>
<div classname={ cx(child,other)}>
My background is inherited
</div>
</div>
</div>
);
}
将第二个类与子类一起添加会破坏样式,因为父类需要该子类名称。但是,从我在其他地方看到的内容来看,这应该可行。我是在做错什么吗?