一、引入classnames库,安装:
npm install classnames --save
二、基本使用:
import classnames from 'classnames'
<div className=classnames({
'class1': true,
'class2': true
)>
</div>
classNames()中的对象,只要value为true时,就会返回这个key(这个类名),所以这里的value可以做很多操作,变量控制,或者用赋值===进行判断,用法比较灵活
三、其他用法
classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
四、传入对象数组
var arr = ['b', { c: true, d: false }];
classNames('a', arr); // => 'a b c'