简体   繁体   English

在反应中的一个元素中添加两个烤肉串案例 css class

[英]Adding two kebab case css class in an element in react

import React from 'react';
import styles from './stylesheet.moudle.css'

<div className={styles['first-style'] styles['second-style']}>
some content
</div>

How do I add styles['second-style'] into the className?如何将styles['second-style']添加到类名中? I tried comma and it does not work.我试过逗号,它不起作用。

Solved it with:解决了它:

<div className={`${styles['first-style']} ${styles['second-style']}`}>

I prefer using classnames npm module https://www.npmjs.com/package/classnames我更喜欢使用类名 npm 模块https://www.npmjs.com/package/classnames

You can do more complex things with this in a clean way.你可以用这种干净的方式做更复杂的事情。

import classNames from 'classNames'

const condition = true;

<div className={
  classNames(
     styles.first-style,
     styles.second-style,
     {'styles.some-other-conditional-classname': condition})}>

You can also use clsx .您也可以使用clsx

const Toolbar = ({
  className,
  ...rest
}) => {
    <Component
      className={clsx(classes.root, className)}
      {...rest}
    >
    ...
}

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

相关问题 如果添加类…,CSS 3 Transition是否不能与Template元素(HTML 5)一起使用? - Is CSS 3 Transition do not work with Template element (HTML 5) in case of Adding Class…? React 样式:Wrapper 组件生成`不支持将 kebab-case 用于对象中的 css 属性。 您是说 ariaLabel?` 错误 - React styled: Wrapper component produces `Using kebab-case for css properties in objects is not supported. Did you mean ariaLabel?` error camelCase 到 kebab-case - camelCase to kebab-case 改变烤肉串案例正则表达式 - Altered kebab case Regex 使用 Next js 时在 CSS/SASS 模块中使用 kebab-case CSS 类名 - Using kebab-case CSS classnames in CSS/SASS modules when using Next js 在 css-loader 3.4.2 中通过 localsConvention 将 kebab-case 转换为 camelCase 不工作 - kebab-case to camelCase via localsConvention in css-loader 3.4.2 not working 破折号案例(Kebab案例)数据绑定在角度2 - Dash case (Kebab case) data binding in angular 2 Angular指令未将CSS类添加到另一个元素 - Angular directive is not adding css class to another element 将 class 添加到元素以在 Z9E13B69D1D2DA927Z7777102ACAAAF7154A3 中运行 CSS animation - Adding a class to an element to run CSS animation in Javascript 使用 React 片段将内联 CSS 添加到 JS class - Adding inline CSS to JS class with React Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM