在使用样式化组件时,我注意到了两种常见方法:
const MyComponent = styled.div`
display: inline-block;
position: relative;
.MyChildComponent {
position: absolute;
background: red;
}
.MySubComponent {
background: yellow;
}
`
// In use
<MyComponent>
<span className="MyChildComponent>A goose!</span>
<button className="MySubComponent">Click bait</button>
</MyComponent>
const MyComponent = styled.div`
display: inline-block;
position: relative;
`
const MyChildComponent = styled.span`
position: absolute;
background: red;
`
const MySubComponent = styled.button`
background: yellow;
`
// In use
<MyComponent>
<MyChildComponent>A Goose!<MyChildComponent>
<MySubComponent>Click bait</MySubComponent>
<MyComponent>
很显然,在某些情况下存在混合和匹配这些方法的情况,并且我没有考虑使用案例,在这些案例中,可能会在整个代码库中重复使用组件。相反,我是在无法修改或共享的大块标记的情况下询问。