[英]Css classname not applied to react component
I appreciate your time and help.我感谢您的时间和帮助。 I spent hours trying to figure this out can't seem to get to the bottom of it.
我花了几个小时试图弄清楚这一点似乎无法追根究底。
I have this react component:我有这个反应组件:
import styles from './style.scss';
class ButtonComponent extends React.Component {
render() {
return (
<div className={styles.bunny}>
hello world
</div>
);
}
}
and scss file:和 scss 文件:
.bunny {
display: block;
margin-top:50px;
margin-bottom:55px;
background-color: blue;
}
When I load up my html, the div has no classname when I expect it to be <div class="bunny">
当我加载我的 html 时,div 没有类名,而我希望它是
<div class="bunny">
When I put the class name in the react component literally like this:当我将类名放在 react 组件中时,就像这样:
<div className="bunny">
then I can see the className in the browser.然后我可以在浏览器中看到 className。
What am I doing wrong?我做错了什么?
Thank you so much in advance.非常感谢你。
首先,你需要保证它是用以下console.log(styles)
定义的: console.log(styles)
我不确定你是否正确导入了 style.scss。
You need to import your scss like... import './styles.scss';
您需要导入您的 scss,例如...
import './styles.scss';
Webpack will take care of bundling your styles so you can add the className as a string on your component. Webpack 将负责捆绑您的样式,以便您可以将 className 作为字符串添加到您的组件上。
<div className="bunny">Hello World</div>
为我解决这个问题的是将我的 scss 文件的名称从style.scss
为style.module.scss
对我来说,这是因为我使用 commonjs 的require
来加载 es 模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.