preface
I transferred from Vue to learn React. When I was used to writing modular CSS via Scoped, I felt very comfortable. However, when I used React, I needed to configure it by myself
The effect
// todolist.scss
.todo_list{
color: green;
}
Copy the code
// TodoList.jsx
import React from 'react'
import Style from './todolist.scss'
class TodoList extends React.Component {
constructor(props) {
super(props)
console.log(1)
}
render() {
return (
<div className={Style.todo_list}>Hello World</div>)}}export default TodoList
Copy the code
Package generated CSS format [file name]__[class name]__[five-digit hash]
code
- perform
npm run eject
Display the configuration file (note: this is not reversible) yarn add node-sass --dev
- Into the
/config/webpack.config.js
The lookupsass-loader
addmodules:{localIdentName:'[name]__[local]__[hash:5]'}
{
test: sassRegex,
exclude: sassModuleRegex,
use: getStyleLoaders(
{
importLoaders: 3.sourceMap: isEnvProduction && shouldUseSourceMap,
modules: {localIdentName:'[name]__[local]__[hash:5]'}},'sass-loader'),}Copy the code
- Start or restart the project