简体   繁体   English

create-react-app 中的 css 模块 localIdentName

[英]css module localIdentName in create-react-app

Is there a default way to set localIdentName for css modules without ejecting?是否有默认方法可以在不弹出的情况下为 css 模块设置localIdentName

I couldn't find such an option on the official docs so I decided to ask here if someone by any chance knows a way without ejecting and tempering with the webpack.config.js .我在官方文档上找不到这样的选项,所以我决定在这里问问是否有人知道一种无需弹出和调整webpack.config.js

You do not need to eject.您不需要弹出。

Create-React-App supports css modules right out of the box as of version 2, which is now stable.从版本 2 开始,Create-React-App 支持开箱即用的 css 模块,该版本现已稳定。

Upgrade to v2 (react-scripts@latest) by running yarn upgrade react-scripts@latest.通过运行 yarn upgrade react-scripts@latest 升级到 v2 (react-scripts@latest)。

You just have to create a file with the extension .module.css你只需要创建一个扩展名为 .module.css 的文件

For example:例如:

.myStyle {
  color: #fff
}

Then you can use it like so:然后你可以像这样使用它:

import React from 'react'
import styles from 'mycssmodule.module.css'

export default () => <div className={styles.myStyle}>We are styled!</div>

To read more - https://facebook.github.io/create-react-app/docs/adding-a-css-modules-stylesheet阅读更多 - https://facebook.github.io/create-react-app/docs/adding-a-css-modules-stylesheet

Is there a default way to set localIdentName for css modules without ejecting?有没有默认方法可以为CSS模块设置localIdentName而不弹出?

I couldn't find such an option on the official docs so I decided to ask here if someone by any chance knows a way without ejecting and tempering with the webpack.config.js .我在官方文档中找不到这样的选项,所以我决定在这里问是否有人偶然知道一种方法而不用webpack.config.js弹出和webpack.config.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM