【发布时间】: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