简体   繁体   English

React:如何在三元组中添加多个样式类?

[英]React: how do I add multiple styling classes within a ternary?

I am using makeStyles within my component, but want to add a ternary to switch between 2 different styles.我在我的组件中使用makeStyles ,但想添加一个三元组在 2 个不同的 styles 之间切换。

I have the following:我有以下内容:

Import ing classNames导入类名

import classNames from 'classnames';

And using it within my component:并在我的组件中使用它:

            <Badge
              className={
                badgeProps.badgeContent === ''
                  ? classNames(classes.MuiBadge, classes.MuiBadgeDotted)
                  : classNames(classes.MuiBadge, classes.MuiBadgeNumber)
              }

But none of these work.但这些都不起作用。 Inspecting the element I don't see any classes appearing.检查元素我没有看到任何类出现。

Would anyone know how I could achieve this?有谁知道我怎么能做到这一点?

Not 100% sure but first try with import classnames from "classnames"不是 100% 确定,但首先尝试import classnames from "classnames"

And second you can do其次你可以做

classnames(classes.MuiBadge, {
    [classes.MuiBadgeDotted]: badgeProps && !badgeProps.badgeContent,
    [classes.MuiBadgeNumber]:  badgeProps && badgeProps.badgeContent
})

Refer for more info https://www.npmjs.com/package/classnames#usage有关更多信息,请参阅https://www.npmjs.com/package/classnames#usage

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

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