【问题标题】:nextjs add dynamic classNames with static classNames [duplicate]nextjs用静态类名添加动态类名[重复]
【发布时间】:2021-10-25 11:57:18
【问题描述】:

我刚刚开始使用 nextjs。要动态添加 CSS 类名,这就是我正在做的事情

<div className={selected? [styles.list,styles.half].join(" ") : styles.list}>

现在styles.list 在 if/else 中都是通用的。

我试过了

<div className={styles.list} className={selected? styles.half : ''}>

但这表明

JSX elements can not have multiple atrributes with same name

有什么方法可以达到同样的效果吗?

【问题讨论】:

  • 错误是不言自明的,你不能有多个同名的属性,在这种情况下className
  • 是的,但是有什么办法可以做到吗?
  • 可能是这样的? className={`${styles.list} ${selected ? styles.half : ''}`}

标签: javascript css next.js jsx


【解决方案1】:

根据我的评论,您可以尝试以下语法:

className={`${styles.list} ${selected ? styles.half : ''}`}

【讨论】:

    【解决方案2】:

    我认为这对你有用

    <div className={`${styles.list} ${selected? styles.half : ''}`}>
    

    【讨论】:

      猜你喜欢
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      相关资源
      最近更新 更多