react官方推薦的classnames庫

一、爲什麼使用classnames這個庫

在react開發中,我們有的時候需要使用js來動態判斷是否爲組件添加class(類名),這裏我們使用到了classnames

二、學習網址

https://www.npmjs.com/package/classnames
https://github.com/JedWatson/classnames

三、安裝與引入

安裝

npm install classnames --save

引入

import classnames  from ‘classnames’;

四、使用示例

<Button className={classnames({
    //這裏可以根據各屬性動態添加,如果屬性值爲true則爲其添加該類名,如果值爲false,則不添加。這樣達到了動態添加class的目的
      base: true,
      inProgress: this.props.store.submissionInProgress,
      error: this.props.store.errorOccurred,
      disabled: this.props.form.valid,
    })}>
<Button/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章