className的写法

本文介绍了在React中处理className的三种方式,包括利用模板字符串、数组join和classnames库。当尝试将样式命名为'search-btn'时遇到问题,通过示例代码展示了如何正确引用和组合多个样式类名。同时,文章还演示了直接使用和结合style引用的方式应用多个className。
摘要由CSDN通过智能技术生成

写法一:命名为search-btn时,会报错,写法如下:

import style from './style.less';

  <Button
                    className={style['search-btn']}
                    type="primary"
                    htmlType="submit"
                  >
                    搜索
                  </Button>

className多种样式的三种写法:

1、 className 利用 ${ }

   className={`${style.fruit} ${style.apple}`}


2、 className join

   className={[style.fruit,style.apple].join(' ')}


3、利用classnames库

  npm install classnames --save
  <div className=classnames({
    'class1': true,
    'class2': true
    )>
</div>


项目中使用classname

使用style引用:

import classnames from 'classnames/bind';
import style from './index.less';
const cx = classnames.bind(style);


<div className={cx('message-read')} >Classname 使用style</div>
<div className={cx('message-read1','message-read2')} >多个classname使用style</div>

 直接使用多个classname使用,不使用style


import './index.less';

<div className='message-read1','message-read2' >直接使用多个classname使用</div>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值