How to add a class with React.js? How to add a class with React.js? jquery jquery

How to add a class with React.js?


It is simple.take a look at this

https://codepen.io/anon/pen/mepogj?editors=001

basically you want to deal with states of your component so you check the currently active one. you will need to include

getInitialState: function(){}//and isActive: function(){}

check out the code on the link


this is pretty useful:

https://github.com/JedWatson/classnames

You can do stuff like

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 typesclassNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'// other falsy values are just ignoredclassNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'

or use it like this

var btnClass = classNames('btn', this.props.className, {  'btn-pressed': this.state.isPressed,  'btn-over': !this.state.isPressed && this.state.isHovered});


Taken from their site.

render() {  let className = 'menu';  if (this.props.isActive) {    className += ' menu-active';  }  return <span className={className}>Menu</span>}

https://reactjs.org/docs/faq-styling.html