react-css-modules

蔚落 2021-09-17 08:20 246阅读 0赞

在开发 react 项目中,有些项目需要设置大量的样式文件,如果通过定义样式常量,纯嵌入的方式编写样式,这将会大大的降低效率。stylus + react-css-modules 是一套可以提高样式编写效率的方案。stylus 是一套 css 预处理框架,而react-css-modules 则实现了样式的模块化。

Stylus参考实例:https://github.com/smallH/react-stylus.git

那么我们一步步来,在使用 stylus 前需要先安装相应依赖包:

安装stylus及其依赖包:

  1. $ npm install stylus -S
  2. $ npm install stylus-loader -S

在react项目中使用stylus,需要将其配置文件暴露出来,到项目根目录下,执行脚本(执行前记得先提交代码备份,以防万一):

  1. $ npm run eject

执行这段脚本时,会询问您是否确认暴露,该过程是不可逆的,选择 ‘Y’ 即可。若在执行过程发生错误,会提示错误信息。常见的错误是提示你需要删掉项目中的跟踪文件,如使用HBuilder编译器时会随项目创建时生成.project跟踪文件。

在配置文件暴露出来后,打开config/webpack.config.dev.js,修改配置如下:

  1. module: {
  2. strictExportPresence: true,
  3. rules: [
  4. ...
  5. oneOf: [
  6. ...
  7. {
  8. test: /\.styl$/,
  9. loaders: ['style-loader', 'css-loader', 'stylus-loader']
  10. },
  11. {
  12. // Exclude `js` files to keep "css" loader working as it injects
  13. // its runtime that would otherwise be processed through "file" loader.
  14. // Also exclude `html` and `json` extensions so they get processed
  15. // by webpacks internal loaders.
  16. exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/, /\.styl$/],
  17. loader: require.resolve('file-loader'),
  18. options: {
  19. name: 'static/media/[name].[hash:8].[ext]',
  20. },
  21. }
  22. ]
  23. ]
  24. ...
  25. }

打开config/webpack.config.prod.js,按上面的办法同样修改一遍,这样build出来的文件.styl文件才会有效。

(小贴士)若想要build后能双击打开index.html就可以看到页面,则把修改publicPath为:

  1. const publicPath = './';

执行以下代码测试效果:

  1. // base.styl
  2. div{
  3. color: red;
  4. p {
  5. color: blue;
  6. }
  7. }
  8. // App.js
  9. import React from 'react';
  10. import './base.styl';
  11. class App extends React.Component {
  12. render() {
  13. return(
  14. <div>
  15. Hello World!
  16. <p>This is Paragraph!</p>
  17. </div>
  18. );
  19. }
  20. }
  21. export default App;

启动项目,效果如图所示:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plcGluZzg5MTEwMw_size_16_color_FFFFFF_t_70

这样表示项目中已经可以使用Stylus。但这种引用方法有个缺点,所引用的样式将会是全局样式,所有的组件都会影响到,这种效果并不是我们想要的,所以这时候我们需要用到样式模块化管理 react-css-modules

安装 react-css-modules:

  1. $ npm install react-css-modules -S

再次打开config/webpack.config.dev.js,修改配置如下:

  1. {
  2. test: /\.styl$/,
  3. loaders: [
  4. 'style-loader',
  5. 'css-loader?modules&localIdentName=[name]-[hash:base64:5]',
  6. 'stylus-loader'
  7. ]
  8. },
  9. {
  10. // Exclude `js` files to keep "css" loader working as it injects
  11. // its runtime that would otherwise be processed through "file" loader.
  12. // Also exclude `html` and `json` extensions so they get processed
  13. // by webpacks internal loaders.
  14. exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/, /\.styl$/],
  15. loader: require.resolve('file-loader'),
  16. options: {
  17. name: 'static/media/[name].[hash:8].[ext]',
  18. },
  19. }

这样就可以使用 react-css-modules 实现对样式的模块化管理了,需要了解 react-css-modules 的api可以到官网去看一下文档 ,官网地址:https://www.npmjs.com/package/react-css-modules。下面是一个简单是使用实例:

  1. import React from 'react'
  2. import styles from './index.styl'
  3. import CSSModules from 'react-css-modules'
  4. import { connect } from 'react-redux'
  5. import * as AllActions from '@/reducers/Auth/actions'
  6. class Login extends React.Component {
  7. constructor(props) {
  8. super(props)
  9. this.state = {}
  10. }
  11. visitor = () => {
  12. // some code about visitor
  13. }
  14. manager = () => {
  15. // some code about manager
  16. }
  17. render() {
  18. return(
  19. <div id="login" styleName="login">
  20. <div styleName="title">欢迎您</div>
  21. <div styleName="nav">
  22. <div styleName="btn" onClick={this.visitor}>游客</div>
  23. <div styleName="btn manager" onClick={this.manager}>管理员</div>
  24. </div>
  25. </div>
  26. );
  27. }
  28. }
  29. const CSSLogin = CSSModules(Login, styles, {
  30. allowMultiple: true
  31. });
  32. // connect 是redux中的模块,如果没有用到则直接导出CSSLogin即可
  33. export default connect(null, AllActions)(CSSLogin)

样式文件:

  1. // index.styl
  2. .login{
  3. .title {
  4. text-align: center;
  5. font-size: 46px;
  6. font-weight: bold;
  7. margin-top: 200px;
  8. }
  9. .nav {
  10. display: flex;
  11. margin-top: 50px;
  12. justify-content: center;
  13. align-items: center;
  14. }
  15. .btn {
  16. min-width: 140px;
  17. text-align: center;
  18. font-size: 24px;
  19. cursor: pointer;
  20. border-radius: 30px;
  21. background-color: #009688;
  22. color: white;
  23. width: fit-content;
  24. padding: 10px;
  25. margin-top: 0px;
  26. }
  27. .manager {
  28. margin-left: 20px;
  29. color: gray;
  30. background-color: transparent;
  31. border: solid 1px #009688;
  32. }
  33. }

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读