Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

reactjs - React App with css module multiple classes

i created a basic react app like this:

import React from 'react';
import style from './Button.module.scss';


export default class Button extends React.Component {
    render() {
        return (
            <button className={[style.class, 'awesome', 'great'].join(' ')}>
                hello world
            </button>
        );
    }
}

the css/scss:

.class {
    background: pink;
    color: red;


    /* not working */
    &:is(.awesome) {
        border-width: 2px;
    }

    /* not working either */
    &.awesome {
        border-width: 2px;
    }

    /* works */
    &:not(.great) {
        border-style: dotted;
    }
}

the problem: the sublass .awesome is not working, whereas .great works fine. Can you fix the code so the .awesome will work. I need some subclass of the .button, so i can toggle them at runtime.

this is the generated css on the browser, the .awesome is not generated but .great generated.

.Button_class__1tDJY:not(.Button_great__3yeAv) {
    border-style: dotted;
}
.Button_class__1tDJY {
    background: pink;
    color: red;
}
question from:https://stackoverflow.com/questions/66067020/react-app-with-css-module-multiple-classes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you should pass the classes declared at your css modules through your styles object, instead of passing a string:

      <button className={[styles.class, styles.awesome, styles.great].join(' ')}>
          hello world
      </button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...