我们是否应该在React样式组件中使用BEM(块,元素,修饰符)
我正在使用React样式组件。我们应该在样式组件中使用BEM(块,元素,修饰符)吗?如果是,那怎么样?
我试图给出className
,但它不起作用
import { SiteHeader } from './site-header';
export const SiteHeaderStyled = styled(SiteHeader).attrs({
className: 'SiteHeader',
})`
&__nav {
color: red;
}
`;
I am working with react styled component. Should we use BEM(Block, Element, Modifier) in styled component ? If yes then how ?
I tried to give ClassName
but its not working
import { SiteHeader } from './site-header';
export const SiteHeaderStyled = styled(SiteHeader).attrs({
className: 'SiteHeader',
})`
&__nav {
color: red;
}
`;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您自己的,您只能使用样式组件来定义每个需要样式的节点,或者您可以混合两者:使用样式组件 +内联样式,用于组件容器 + ClassName的内部节点的样式组件(具有定义的样式)在样式的容器中),甚至根据情况混合这3种方法。
在每种情况下,如果您坚持使用样式组件,则不需要bem,因为至少一切都范围仅范围为1个组件
编辑:对您的编辑响应
什么是不起作用的?在SiteHeaderstyled(.SiteHeader)还是Nav Child上的样式?
& __ nav
由于没有父母而无法工作(在SCSS代码部分中),您必须像这样包装它:通过上述使用如屏幕截图
无论如何,如果这是您在各处使用样式组件的项目,那么类似的事情就应该足够了:
That's on your own, you could only use styled-components to define every node that needs style, or you could mix the two: use styled-components + inline style, styled-components for component container + classname for inner nodes (with style defined in the styled container), or even mix these 3 approaches depending on the case.
In every case, if you stick with styled-component, you don't need BEM because everything is, at least, scoped to only 1 component
EDIT: response to your edit
What's not working ? style on SiteHeaderStyled (.SiteHeader) or on the nav child ?
The
&__nav
can't work because there is no parent (in the scss code part), you have to wrap it like this:By using above is coming like screenshot
in any case, if it's a project where you use styled-component everywhere, something like this is should be sufficient: