【问题标题】:using material-ui with SCSS directly in React component直接在 React 组件中使用 Material-ui 和 SCSS
【发布时间】:2018-07-31 23:13:52
【问题描述】:
我有一个关于在 React 组件中使用样式的问题。我想使用 SCSS 而不是使用 withStyle 函数或将纯 CSS 文件导入 JS 文件。
例如,我想将我的 SCSS 文件导入到ButtonComponent.js,如下所示:
import './buttonStyle.scss'
- 将我的主题配置放在名为
theme.scss 的 SCSS 文件中
- 将
theme.scss 导入到我的 SCSS 文件中
- 利用组件的
className 中的SCSS 类。
我该怎么做?
【问题讨论】:
标签:
javascript
css
reactjs
sass
material-ui
【解决方案1】:
您将需要使用带有加载器的捆绑器,该加载器可以将 scss 编译成 css。
您可以将 webpack 与 sass-loader 一起使用。最小配置如下所示:
// webpack.config.js
var path = require('path');
module.exports = {
entry: './foo.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'foo.bundle.js'
},
module: {
rules: [{
test: /\.scss$/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
}, {
loader: "css-loader" // translates CSS into CommonJS
}, {
loader: "sass-loader" // compiles Sass to CSS
}]
}]
}
};
您可以阅读有关 webpack 入门的更多信息here。