jsx动态class写法:vue3与react+classname库
Author:zhoulujun Date:
写惯了vue2来说,回到jsx,觉得className写的很不习惯
https://www.npmjs.com/package/classnames
这个库可以想vue2里面一样的用法
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' // lots of arguments of various types classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux' // other falsy values are just ignored classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
用起来非常方便
var classNames = require('classnames/dedupe'); class Button extends React.Component { // ... render () { var btnClass = classNames({ btn: true, 'btn-pressed': this.state.isPressed, 'btn-over': !this.state.isPressed && this.state.isHovered }); return <button className={btnClass}>{this.props.label}</button>; } }
不用多讲哈
转载本站文章《jsx动态class写法:vue3与react+classname库》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/jsBase/2019_1213_8711.html