简体   繁体   English

如何使用css模块而不修改react中的类名

[英]how to use css module without modifying class names in react

I am planning on using css modules with my react project and i quickly found out how ugly things can get when one is required to start changing classnames to use the css modules我计划在我的 react 项目中使用 css 模块,我很快发现当人们需要开始更改类名以使用 css 模块时,事情会变得多么丑陋

lets say i have a component using regular css假设我有一个使用常规 css 的组件

import React from 'react'
import "./styles.css";

const About = () => {
    return (
      <div>
        <h1 class="title">CSS Modules</h1>
          <span class="select2-selection select2-selection--single dropdown-container" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-static-cooldown-container">
          <span class="select2-selection__rendered" id="select2-static-cooldown-container" role="textbox" aria-readonly="true" title="1 hour">
            <span>
              <div class="icon icon-time-2"></div> 1 hour
            </span>
          </span>
          </span>

      </div>
    );
};

export default Dashboard;

but when i am using css modules in a component, i have to modify the class names但是当我在组件中使用 css 模块时,我必须修改类名

import React from 'react'
import styles from "./styles.module.css";

const About = () => {
    return (
      <div>
        <h1 class={styles.title}>CSS Modules</h1>
          <span class="select2-selection select2-selection--single dropdown-container" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-static-cooldown-container">
          <span class="select2-selection__rendered" id="select2-static-cooldown-container" role="textbox" aria-readonly="true" title="1 hour">
            <span>
              <div class="icon icon-time-2"></div> 1 hour
            </span>
          </span>
          </span>

      </div>
    );
};

export default Dashboard;

How should this be?这应该如何?

besides the class name, what about the other css elements/attributes like id?除了类名,其他 css 元素/属性如 id 呢? et cetera.等等。 What about multiple classes in the class name?类名中有多个类呢? Will i have to modify all of those too?我是否也必须修改所有这些?

is there a way to use css modules without modifying class names?有没有办法在不修改类名的情况下使用 css 模块?

I have looked around and haven't found anything, most people just follow along and continue to modify class names but a part of me is thinking there has to be a better way because that is really ugly我环顾四周并没有找到任何东西,大多数人只是跟着并继续修改类名,但我的一部分认为必须有更好的方法,因为那真的很丑

If you really want to see similar code you can make:如果你真的想看到类似的代码,你可以:

const About = () => {
    const {title} = styles;

    return (
      <div>
        <h1 className={title}>CSS Modules</h1>
      </div>
    );
};

so now you only change "" for {}所以现在您只需为{}更改""

you can use Array.join for multiple classes to space them您可以将Array.join用于多个类来Array.join它们

const About = () => {
    const {title,otherClass} = styles;

// const className = [title,otherClass].join(' ');

      return (
      <div>
        <h1 className={[title,otherClass].join(' ')}>CSS Modules</h1>
      </div>
    );
};

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

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