javascript/ css/ reactjs

So sometimes I would group 2 selectors in the className at the same time like <div className="slectorA selectorB" /> which would render .selectorA .selectorB { (...) } in the CSS file if I am not mistaken.

I was wondering how to do that if I were to use a CSS module. Define styles = './App.module.css' then how would I do the same call as before? Here is an example of what I am trying to do in my code.

App.js

import styles from "./App.module.css";

const manageBackgroundImage = (temperature) => {
  if (temperature > 16) return "{styles.weather_box.warm}";
  else return "{styles.weather_box.cold}";
  //(...more else ifs here...)
};

export default function App() {
  const temperature = 17;

  return (
    <div>
      <div className={manageBackgroundImage(temperature)}></div>
    </div>
  );
}

App.module.css

.weather_box {
    display: grid;
    width: 40%;
    height: inherit;
}

.weather_box.cold {
    background-image: url('../../assets/cold.png');
    transition: 0.6s ease-out;
}

.weather_box.warm {
    background-image: url('../../assets/warm.png');
    transition: 0.6s ease-out;
}

App.js

import styles from "./App.module.css";
const manageBackgroundImage = (temperature) => {
  if (temperature > 16) return `${styles.weather_box} ${styles.warm}`;
  else return `${styles.weather_box} ${styles.cold}`;
};

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question How to group the css selectors Normal string className in CSS module Multiple CSS Selectors with Prototype How to combine css selectors How to remove classname using multiple classname in JavaScript Way to apply a :not against a group of element css selectors? how to apply multiple selectors? How to add wildcard to CSS selectors? How to compare two CSS selectors? jQuery: Having trouble with not and multiple selectors for CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM