React中classnames庫使用示例
classnames的引入
從名字上可以看出,這個(gè)庫是和類名有關(guān)的。官方的介紹就是一個(gè)簡單的支持動(dòng)態(tài)多類名的工具庫。
使用 npm安裝
npm install classnames
使用 Bower安裝
bower install classnames
使用 Yarn安裝
yarn add classnames
引入
import classnames from ‘classnames';
使用 Node.js, Browserify, or webpack:
var classNames = require('classnames');
classNames('foo', 'bar'); // => 'foo bar'classnames函數(shù)的使用
classNames 函數(shù)接受任意數(shù)量的參數(shù),可以是string、boolean、number、array、dictionary等。
type ClassValue = string I number I ClassDictionary I ClassArray I undef inednull I boolean;
參數(shù) 'foo' 是 { foo: true } 的縮寫。如果與給定鍵關(guān)聯(lián)的值是false的,則該key值將不會(huì)包含在輸出中。
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'
數(shù)組的形式
數(shù)組可以按照上面的規(guī)則,遞歸展開數(shù)組中的每一項(xiàng):
var arr = ['b', { c: true, d: false }];`
classNames('a', arr); // => 'a b c'
ES6中使用動(dòng)態(tài)類名
let buttonType = 'primary';`
classNames({ [`btn-${buttonType}`]: true });
結(jié)合React一起使用
這個(gè)包是classSet的官方替代品,她最初是在React.js插件包中提供的。
常見的一個(gè)應(yīng)用場景是根據(jù)條件動(dòng)態(tài)設(shè)置類名,在React中是會(huì)寫出如下的代碼:
class Button extends React.Component {
// ...
render () {
var btnClass = 'btn';
if (this.state.isPressed) btnClass += ' btn-pressed';
else if (this.state.isHovered) btnClass += ' btn-over';
return <button className={btnClass}>{this.props.label}</button>;
}
}
使用classnames可以通過對象非常方便的寫出條件多類名。
var classNames = require('classnames');
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>;
}
}
因?yàn)榭梢詫ο?、?shù)組和字符串參數(shù)混合在一起,所以支持可選的 className props 也更簡單,因?yàn)橹挥姓鎸?shí)的參數(shù)才會(huì)包含在結(jié)果中:
var btnClass = classNames('btn', this.props.className, {
'btn-pressed': this.state.isPressed,
'btn-over': !this.state.isPressed && this.state.isHovered
});
總結(jié):
在React項(xiàng)目中使用classnames是非常方便我們管理動(dòng)態(tài)多類名。為我們的工作真正的提效。
以上就是React中classnames庫使用示例的詳細(xì)內(nèi)容,更多關(guān)于React classnames庫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文搞懂?React?18?中的?useTransition()?與?useDeferredValue()
這篇文章主要介紹了一文搞懂?React?18?中的?useTransition()與useDeferredValue(),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
react-native DatePicker日期選擇組件的實(shí)現(xiàn)代碼
本篇文章主要介紹了react-native DatePicker日期選擇組件的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,有興趣的可以了解下2017-09-09
React語法中設(shè)置TS校驗(yàn)規(guī)則的步驟詳解
這篇文章主要給大家介紹了React語法中如何設(shè)置TS校驗(yàn)規(guī)則,文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10
React實(shí)現(xiàn)單向數(shù)據(jù)流的方法
本文主要介紹了React實(shí)現(xiàn)單向數(shù)據(jù)流的方法2023-04-04
解決React報(bào)錯(cuò)Property 'X' does not 
這篇文章主要為大家介紹了解決React報(bào)錯(cuò)Property 'X' does not exist on type 'HTMLElement',有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React實(shí)現(xiàn)響應(yīng)式布局的最佳實(shí)踐
在當(dāng)今的前端開發(fā)中,響應(yīng)式設(shè)計(jì)已經(jīng)成為必不可少的部分,隨著各種設(shè)備的出現(xiàn),確保我們的網(wǎng)頁在不同屏幕尺寸上展示良好,已經(jīng)成為開發(fā)者的首要任務(wù)之一,本文將介紹如何在React中實(shí)現(xiàn)響應(yīng)式布局,提供一些最佳實(shí)踐和示例代碼,幫助大家更好地理解這一概念2025-02-02
學(xué)習(xí)ahooks useRequest并實(shí)現(xiàn)手寫
這篇文章主要為大家介紹了學(xué)習(xí)ahooks useRequest并實(shí)現(xiàn)手寫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

