Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple classes to a ReactJS Component?

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 each li:

<li key={index} className={activeClass, data.class, "main-class"}></li> 

My React component is:

var AccountMainMenu = React.createClass({   getInitialState: function() {     return { focused: 0 };   },    clicked: function(index) {     this.setState({ focused: index });   },    render: function() {     var self = this;     var accountMenuData = [       {         name: "My Account",         icon: "icon-account"       },       {         name: "Messages",         icon: "icon-message"       },       {         name: "Settings",         icon: "icon-settings"       }     /*{         name:"Help &amp; Support &nbsp; <span class='font-awesome icon-support'></span>(888) 664.6261",         listClass:"no-mobile last help-support last"       }*/     ];      return (       <div className="acc-header-wrapper clearfix">         <ul className="acc-btns-container">           {accountMenuData.map(function(data, index) {             var activeClass = "";              if (self.state.focused == index) {               activeClass = "active";             }              return (               <li                 key={index}                 className={activeClass}                 onClick={self.clicked.bind(self, index)}               >                 <a href="#" className={data.icon}>                   {data.name}                 </a>               </li>             );           })}         </ul>       </div>     );   } });  ReactDOM.render(<AccountMainMenu />, document.getElementById("app-container")); 
like image 525
Hector Avatar asked Dec 30 '15 01:12

Hector


People also ask

How do you add multiple classes in Reactjs?

Adding a single class name import React from 'react'; import './styles. css'; function App(){ return ( <div className="container"> <h1>Hello rock!! </h1> </div> ) } export default App; If you want to add multiple class names, you can add it to the className attribute by separating with spaces.

Can a component have multiple classes?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.

How do you add multiple classes in React material UI?

Use clsx to add multiple classes We are going to use the clsx npm library to add multiple classes to the component. import clsx from 'clsx'; Latest version of React material UI already has the clsx included in their library so you don't have to separately install it.

How do you add multiple classes?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.


Video Answer


2 Answers

I use ES6 template literals. For example:

const error = this.state.valid ? '' : 'error' const classes = `form-control round-lg ${error}` 

And then just render it:

<input className={classes} /> 

One-liner version:

<input className={`form-control round-lg ${this.state.valid ? '' : 'error'}`} /> 
like image 103
Damian Pavlica Avatar answered Sep 28 '22 07:09

Damian Pavlica


I use classnames when there is a fair amount of logic required for deciding the classes to (not) use. An overly simple example:

...     var liClasses = classNames({       'main-class': true,       'activeClass': self.state.focused === index     });      return (<li className={liClasses}>{data.name}</li>); ... 

That said, if you don't want to include a dependency then there are better answers below.

like image 23
Jack Avatar answered Sep 28 '22 06:09

Jack