我在这里有一个游乐场:https : //codesandbox.io/s/736v9vjzw1
const Something = ({ classes, children, variant }) => {
return (
<div className={classes.someThing}>
<p> I'm some thing </p>
<SomeOtherThing />
<SomeOtherThing> I have some children </SomeOtherThing>
<SomeOtherThing> I have some children </SomeOtherThing>
<SomeOtherThing> I have some children </SomeOtherThing>
</div>
);
};
const styles = {
someThing: {
color: "green",
border: "solid 2px black",
margin: 30,
"& $someOtherThing": {
backgroundColor: "pink" // Doesn't work
},
"& p": {
fontWeight: "bold" //This works but is too broad
}
}
};
我在这里有一个情况,我想SomeOtherThing
在我的SomeThing
.
我可以使用& p
选择器来选择p
元素 - 但我不喜欢这样。它可以设置p
我周围的任何随机s 的样式- 我不想查看组件定义内部来查找它的顶级元素是什么。
我怎样才能做到这一点?类似的东西& SomeOtherElement
。
这个在现实世界的应用,是在我想要的一些地方和其他地方SomeOtherElement
显示。block
inline-block