jsx怎么合并多个classname
在JSX中,可以通过在className属性中使用字符串模板和条件运算符来合并多个classname。
例如,如果要将"button"和"primary"两个类名合并为一个类名,可以使用以下代码:
<button className={`button ${isPrimary ? 'primary' : ''}`}>
Click me
</button>
其中,${isPrimary ? 'primary' : ''}是一个条件运算符,如果isPrimary为true,则返回"primary",否则返回空字符串。
这样,当isPrimary为true时,按钮元素的类名会变成"button primary",否则只有"button"类名。
原文地址: https://www.cveoy.top/t/topic/bBSp 著作权归作者所有。请勿转载和采集!