I am new to ReactJS and JSX and I am having a little problem with the code below.
I am trying to add multiple classes to the className
attribute on eac
If you don't feel like importing another module, this function works like the classNames
module.
function classNames(rules) {
var classes = ''
Object.keys(rules).forEach(item => {
if (rules[item])
classes += (classes.length ? ' ' : '') + item
})
return classes
}
You can use it like this:
render() {
var classes = classNames({
'storeInfoDiv': true,
'hover': this.state.isHovered == this.props.store.store_id
})
return (
<SomeComponent style={classes} />
)
}
Use https://www.npmjs.com/package/classnames
import classNames from 'classnames';
Can use multiple classes using comas seperated:
<li className={classNames(classes.tableCellLabel, classes.tableCell)}>Total</li>
Can use multiple classes using comas separated with condition:
<li className={classNames(classes.buttonArea, !nodes.length && classes.buttonAreaHidden)}>Hello World</li>
Using array as props to classNames will also work, but gives warning e.g.
className={[classes.tableCellLabel, classes.tableCell]}
I don't think we need to use an external package for just adding multiple classes.
I personally use
<li className={`li active`}>Stacy</li>
or
<li className={`li ${this.state.isActive ? 'active' : ''}`}>Stacy<li>
or
<li className={'li ' + (this.state.isActive ? 'active' : '') }>Stacy<li>
the second and third one in case you need to add or remove classes conditionally.
It can be done with https://www.npmjs.com/package/clsx :
https://www.npmjs.com/package/clsx
First install it:
npm install --save clsx
Then import it in your component file:
import clsx from 'clsx';
Then use the imported function in your component:
<div className={ clsx(classes.class1, classes.class2)}>
Late to the party, but why use third party for such a simple problem?
You could either do it as @Huw Davies mentioned - the best way
1. <i className={`${styles['foo-bar-baz']} fa fa-user fa-2x`}/>
2. <i className={[styles['foo-bar-baz'], 'fa fa-user', 'fa-2x'].join(' ')}
Both are good. But writing can become complex for a large app. To make it optimal, I do the same above things but put it in a helper class
Using my below helper function, allows me to keep the logic separate for future editing, and also gives me multiple ways to add the classes
classNames(styles['foo-bar-baz], 'fa fa-user', 'fa-2x')
or
classNames([styles['foo-bar-baz], 'fa fa-user', 'fa-2x'])
This is my helper function below. I've put it in a helper.js where I keep all my common methods. Being such a simple function, I avoided using 3rd party to keep control
export function classNames (classes) {
if(classes && classes.constructor === Array) {
return classes.join(' ')
} else if(arguments[0] !== undefined) {
return [...arguments].join(' ')
}
return ''
}
This is how you can do that with ES6:
className = {`
text-right
${itemId === activeItemId ? 'active' : ''}
${anotherProperty === true ? 'class1' : 'class2'}
`}
You can list multiple classes and conditions and also you can include static classes. It is not necessary to add an additional library.
Good luck ;)